gpt4 book ai didi

python - 使用 futures.ProcessPoolExecutor 但不使用带有 PRAW 包装器的 futures.ThreadPoolExecutor 时的递归最大错误

转载 作者:太空宇宙 更新时间:2023-11-03 11:49:46 24 4
gpt4 key购买 nike

我正在使用这段代码来抓取 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 对象会导致进程而不是线程的递归错误?

最佳答案

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

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

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

关于python - 使用 futures.ProcessPoolExecutor 但不使用带有 PRAW 包装器的 futures.ThreadPoolExecutor 时的递归最大错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30806767/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com