使用futures.ProcessPoolExecutor而不是future.ThreadPoolExecutor使用PRAW包装时的递归最大错误

时间:2015-06-12 15:25:00

标签: python python-multithreading python-multiprocessing praw concurrent.futures

我正在使用此代码来抓取API:

submissions = get_submissions(1)
with futures.ProcessPoolExecutor(max_workers=4) as executor:
#or using this: with futures.ThreadPoolExecutor(max_workers=4) as executor:
    for s in executor.map(map_func, submissions):
        collection_front.update({"time_recorded":time_recorded}, {'$push':{"thread_list":s}}, upsert=True)

它可以很好地/快速地使用线程,但是当我尝试使用进程时,我得到一个完整的队列并出现此错误:

  File "/usr/local/lib/python3.4/dist-packages/praw/objects.py", line 82, in __getattr__
    if not self.has_fetched:
RuntimeError: maximum recursion depth exceeded
Exception in thread Thread-3:
Traceback (most recent call last):
  File "/usr/lib/python3.4/threading.py", line 920, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.4/threading.py", line 868, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/lib/python3.4/concurrent/futures/process.py", line 251, in _queue_management_worker
    shutdown_worker()
  File "/usr/lib/python3.4/concurrent/futures/process.py", line 209, in shutdown_worker
    call_queue.put_nowait(None)
  File "/usr/lib/python3.4/multiprocessing/queues.py", line 131, in put_nowait
    return self.put(obj, False)
  File "/usr/lib/python3.4/multiprocessing/queues.py", line 82, in put
    raise Full
queue.Full

Traceback (most recent call last):
  File "reddit_proceses.py", line 64, in <module>
    for s in executor.map(map_func, submissions):
  File "/usr/lib/python3.4/concurrent/futures/_base.py", line 549, in result_iterator
    yield future.result()
  File "/usr/lib/python3.4/concurrent/futures/_base.py", line 402, in result
    return self.__get_result()
  File "/usr/lib/python3.4/concurrent/futures/_base.py", line 354, in __get_result
    raise self._exception
concurrent.futures.process.BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.

请注意,最初这些流程对于小型数据检索工作效果非常快,但现在它们根本无法正常工作。这是一个错误或者是什么导致PRAW对象会导致进程出现错误而不是使用Threads?

1 个答案:

答案 0 :(得分:1)

我有类似的问题从线程转移到进程只有我使用executor.submit。我认为这可能与您遇到的问题相同,但我无法确定,因为我不知道您的代码在什么情况下运行。

就我而言,发生的事情是:我将我的代码作为脚本运行,而且我没有使用始终推荐的if __name__ == "__main__":。看起来当使用执行程序运行新进程时,python会加载.py文件并运行submit中指定的函数。因为它加载了文件,所以主文件中存在的代码(不在函数内部或上面的句子中)运行,因此每个进程将再次运行一个具有无限递归的新进程。

看起来线程不会发生这种情况。