我正在尝试用 Python 制作一个多进程程序。我已经导入了多进程模块,我尝试像这样开始处理:
p = Process(target=self.Parse)
p.start()
p.join()
在类中,我有一个内部线程计数器,每次产生一个进程时我都会增加计数器。但是当我打印线程计数时,计数不会增加。然后我调用 multiprocessing.active_children() 但这会返回一个空列表。程序真的不会产生线程或进程,还是只是报告它?代码如下:
def run(self):
if self.cont:
while self.nxtLink or (self.thread>1):
print(active_children())
if self.thread<=self.count:
p = Process(target=self.Parse)
p.start()
p.join()
else:
self.crawl(nxtLink.popleft())
解析函数:
def Parse(self):
self.thread+=1
self.lock.acquire()
next = self.nxtLink.popleft()
self.lock.release()
results = parser(next[0],next[1])
#print("In Parse")
self.broken[next[0]] = results.broken
for i in results.foundLinks:
if(self.thread<=self.count+5):
p = Process(target = self.request, args = (i,next[0]))
p.start()
p.join()
else:
while (self.thread>self.count+5):
pass #Waits for the thread count to drop before spawning a new thread.
p = Process(target = self.request, args = (i,next[0]))
p.start()
p.join()
self.lock.acquire()
self.thread-=1
self.lock.release()
最后是请求函数:
def request(self, requestURL, requestingPageURL):
# print(requestURL)
self.lock.acquire()
self.thread+=1
self.lock.release()
try:
before = list(self.prev)
self.lock.acquire()
self.prev.append(requestURL)
self.lock.release()
if(requestURL in before):
#print(before)
return
nextRequest = req.urlopen(requestURL)
self.lock.acquire()
self.nxtLink.append((requestURL,nextRequest))
self.lock.release()
except err.URLError:
self.lock.acquire()
try:
self.broken[requestingPageURL].append(requestURL)
except KeyError:
self.broken[requestingPageURL] = [requestURL]
self.lock.release()
finally:
self.lock.acquire()
self.thread-=1
self.lock.release()
我真的很困惑为什么它没有产生进程但是整个程序运行良好,所以我有点困惑。