Multi-threading. Tarmoqda istisno

misol bilan tushunishga harakat qilaman. Kodi:

import Queue
import threading
import urllib2
import time
from BeautifulSoup import BeautifulSoup

hosts = ["http://yahoo.com", "http://google.com", "http://amazon.com",
"http://ibm.com", "http://apple.com"]

queue = Queue.Queue()
out_queue = Queue.Queue()

class ThreadUrl(threading.Thread):
    """Threaded Url Grab"""
    def __init__(self, queue, out_queue):
        threading.Thread.__init__(self)
        self.queue = queue
        self.out_queue = out_queue

    def run(self):
        while True:
            #grabs host from queue
            host = self.queue.get()

            #grabs urls of hosts and then grabs chunk of webpage
            url = urllib2.urlopen(host)
            chunk = url.read()

            #place chunk into out queue
            self.out_queue.put(chunk)

            #signals to queue job is done
            self.queue.task_done()

class DatamineThread(threading.Thread):
    """Threaded Url Grab"""
    def __init__(self, out_queue):
        threading.Thread.__init__(self)
        self.out_queue = out_queue

    def run(self):
        while True:
            #grabs host from queue
            chunk = self.out_queue.get()

            #parse the chunk
            soup = BeautifulSoup(chunk)
            print soup.findAll(['title'])

            #signals to queue job is done
            self.out_queue.task_done()

start = time.time()
def main():

    #spawn a pool of threads, and pass them queue instance
    for i in range(5):
        t = ThreadUrl(queue, out_queue)
        t.setDaemon(True)
        t.start()

    #populate queue with data
    for host in hosts:
        queue.put(host)

    for i in range(5):
        dt = DatamineThread(out_queue)
        dt.setDaemon(True)
        dt.start()


    #wait on the queue until everything has been processed
    queue.join()
    out_queue.join()

main()
print "Elapsed Time: %s" % (time.time() - start)

Ba'zan men bu xatoni bu yerga keltiraman:

Thread Thread-10-da istisno (tarjimonni o'chirishda ko'pincha ko'tarilgan)

Iltimos, nima sabab bo'lganini tushuntirib bering.

Boshqa muallif tomonidan yangilash:

Mana shunga o'xshash kodda men to'liq istisno:

Exception in thread Thread-1 (most likely raised during interpreter shutdown):
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/threading.py", line 552, in __bootstrap_inner
  File "/usr/local/lib/python2.7/threading.py", line 505, in run
  File "mine.py", line 86, in run
  File "/usr/local/lib/python2.7/Queue.py", line 168, in get
  File "/usr/local/lib/python2.7/threading.py", line 237, in wait
: 'NoneType' object is not callable
4
Odamlar sizga yordam berishga qanchalik tayyor bo'lishlari haqida ba'zi fikrlar: 1. Muammoni tashxislash uchun nima qilayotganingizni va nima uchun harakatlaringiz muvaffaqiyatsiz yakunlanganini tushuntirib bering. 2. Kodni xatolik yuzaga kelishi uchun zarur bo'lgan minimal darajaga kamaytirishga harakat qiling. 3. Xatoning to'liq chizig'ini bering.
qo'shib qo'ydi muallif Sven Marnach, manba
Xuddi shu kodda xuddi shu xatoni ko'rib turibman. Yuqoridagi to'liq istisnoni qo'shaman.
qo'shib qo'ydi muallif Ry4an Brase, manba
Multiprocessing.Pool sinfidan foydalanganda bu xatoni ba'zan topaman. Tarjimon ishni tugatish vaqtida narsalarni o'chirib yuborganda, u hali ham ishlayotgan ishlaydigan ishbog'idan ko'rinadi. Agar echim topilmasa, men kuzatuv ostidagi chastotani kamaytirishni hal qilaman. Bu juda ko'p ishlov berish bilan bajarilganingizda pool.terminate() yoki shunga o'xshash qo'ng'iroqlarni amalga oshirishdir.
qo'shib qo'ydi muallif boboquack, manba

2 javoblar

That is bug http://bugs.python.org/issue14623

Eng oddiy echim - takrorlashni kiritish

time.sleep(1)

skript oxirida hayot tugashiga va yaqinlashishiga sabab bo'ladigan narsalarni tugatishga imkon beradigan skript oxirida

3
qo'shib qo'ydi
Yaxshiroq bo'lsa ham, boshlangan barcha mavzularni kuzatib boring va ularni t.join() deb chaqirib, ular to'g'ri chikilganidan va tozalagandan ishonch hosil qiling.
qo'shib qo'ydi muallif John Szakmeister, manba

Sizning misol ssenariysi yaxshi ko'rinadi, ya'ni python 2.7.2 yordamida men uchun yaxshi ishlaydi.

What version of python are you using? It's possible the errors you're seeing may be related to this bug. If so, then upgrading to python>=2.6.5 or python>=3.1 might help.

1
qo'shib qo'ydi
Python 2.7 da shunga o'xshash muammoga duch kelmoqdaman: thread Thread-9429-da istisno: Traceback (oxirgi chaqiriq so'nggi): "/Library/Frameworks/EPD64.framework/Versions/7.2/lib/python‌ 2.7/threading.py" , layn 552, __bootstrap_inner self.run() faylida "/Library/Frameworks/EPD64.framework/Versions/7.2/lib/python‌ 2.7/threading.py", 756-bet, o'z-o'zidan ishlaydigan (* self. args, ** self.kwargs) TypeError: 'NoneType' obyekti chaqirilmaydi
qo'shib qo'ydi muallif jtlz2, manba
PYTHon 2.7.5 bir xil xato
qo'shib qo'ydi muallif Dmitry Dubovitsky, manba
Ajabo, lekin hatto python2.7 da bir xil xato.
qo'shib qo'ydi muallif glebus, manba
Python
Python
372 ishtirokchilar

Bu guruh python dasturlash tilini muhokama qilish uchun. Iltimos, o'zingizni hurmat qiling va faqat dasturlash bo'yicha yozing. Botlar mavzusini @botlarhaqida guruhida muhokama qling! FAQ: @PyFAQ Offtopic: @python_uz_offtopic

Python offtopic group !
Python offtopic group !
150 ishtirokchilar

@python_uz gruppasining offtop gruppasi. offtop bo'lsa ham reklama mumkin emas ) Boshqa dasturlash tiliga oid gruppalar @languages_programming