我已经检查了各种各样的方法,不知道为什么我的各种过程不会启动并且同时运行谷歌没有错误,因为它确实在大多数情况下工作
from multiprocessing import Process, Queue, Event #for threading
class WebScraper(object):
def __init__(self, pq, e):
self.pdfQueue = pq
self.e = e
def run(self):
i = 0
while True:
print('tweet scraper online')
i = i + 1 #testing
if i == 10: #testing
e.set() #testing TODO remove
if self.e.is_set():
print("killing tweet scraper")
break
class DataParser(object):
def __init__(self, pq, dq, e):
self.pdfQueue = pq
self.dataQueue = dq
self.e = e
self.count = 0
def run(self):
while True:
pdf = self.pdfQueue.get()
print ('parsing pdf')
self.e.set()
if self.e.is_set():
print ('killing database parser')
break
class DatabaseWriter(object):
def __init__(self, dq, e):
self.dataQueue = dq
self.e = e
def run(self):
while True:
print("database writer")
if self.e.is_set():
print ('killing database writer')
break
if __name__ == '__main__':
print('\nStarting script')
e = Event()
pq = Queue()
dq = Queue()
ws = WebScraper(pq, e)
dp = DataParser(pq, dq, e)
dw = DatabaseWriter(dq, e)
p1 = Process(target=ws.run)
p1.start()
p2 = Process(target=dp.run)
p2.start()
p3 = Process(target=dw.run)
p3.start()
p1.join()
p2.join()
p3.join()
输出如下所示,它可以快速运行,没有任何意外,因为您可以看到两个进程
Starting script
tweet scraper online
tweet scraper online
tweet scraper online
tweet scraper online
tweet scraper online
tweet scraper online
tweet scraper online
tweet scraper online
tweet scraper online
tweet scraper online
killing tweet scraper
database writer
killing database writer
如果我删除了WebScraper类中的if语句,WebScraper和DatabaseWriter启动但是DatabaseParser看起来没有运行
答案 0 :(得分:1)
您的self.pdfQueue.get()阻止DataParser实例中的其余方法。见https://docs.python.org/2/library/queue.html#Queue.Queue.get