Im пытается создать простое многопроцессорное приложение с использованием очереди.Python обрабатывает несколько элементов в очереди с использованием рабочих
Im запускает 4 процесса обработки данных с нескольких веб-сайтов. Я хочу, чтобы каждый процесс обрабатывал разные веб-сайты, но по какой-то причине процессы выполняются несколько раз и никогда не завершаются.
from multiprocessing import Process
import Queue
import requests
def readdata(item):
print item
r = requests.get(item)
print 'read data'
print r.status_code
def worker(queue):
while True:
try:
print 'start process'
item = queue.get()
readdata(item)
q.task_done()
except:
print "the end"
break
if __name__ == "__main__":
nthreads = 4
queue = Queue.Queue()
# put stuff in the queue here
moreStuff = ['http://www.google.com','http://www.yahoo.com','http://www.cnn.com']
for stuff in moreStuff:
queue.put(stuff)
procs = [Process(target = worker, args = (queue,)) for i in xrange(nthreads)]
for p in procs:
p.start()
for p in procs:
p.join()
Выход:
start process
http://www.google.com
start process
http://www.google.com
start process
http://www.google.com
start process
http://www.google.com
read data
200
start process
http://www.yahoo.com
read data
200
start process
http://www.yahoo.com
read data
200
start process
http://www.yahoo.com
read data
200
start process
http://www.yahoo.com
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
read data
200
start process
http://www.cnn.com
read data
200
start process
http://www.cnn.com
read data
200
start process
http://www.cnn.com
read data
200
start process
read data
200
start process
http://www.cnn.com
read data
200
start process
read data
200
start process
read data
200
start process
Как проверить, если очередь пуста, и выход?
Спасибо. Я проверяю queue.empty() и прерываю цикл, и он отлично работает .. но я не уверен, почему один и тот же элемент обрабатывается несколько раз. – user1050619
Поскольку очередь не меняется, вы также можете использовать «Пул» и упростить все это, не так ли? – RobertB