小弟最近在使用 python + mongodb + celery 時遇到 RuntimeError 這個問題,想上來問問前輩們有沒有遇過類似的問題或可能的情況
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/celery/app/trace.py", line 451, in trace_task
R = retval = fun(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/celery/app/trace.py", line 734, in __protected_call__
return self.run(*args, **kwargs)
File "/celery_app/tasks.py", line 352, in period_search_tasks
break
File "/usr/local/lib/python3.8/contextlib.py", line 131, in __exit__
self.gen.throw(type, value, traceback)
File "/core/utils/mongodb_utils.py", line 14, in get_db
db_client.close()
File "/usr/local/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1342, in close
self._end_sessions(session_ids)
File "/usr/local/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1310, in _end_sessions
with self._socket_for_reads(ReadPreference.PRIMARY_PREFERRED, None) as (
File "/usr/local/lib/python3.8/contextlib.py", line 113, in __enter__
return next(self.gen)
File "/usr/local/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1476, in _socket_for_reads
topology = self._get_topology()
File "/usr/local/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1385, in _get_topology
self._kill_cursors_executor.open()
File "/usr/local/lib/python3.8/site-packages/pymongo/periodic_executor.py", line 86, in open
thread.start()
File "/usr/local/lib/python3.8/threading.py", line 852, in start
_start_new_thread(self._bootstrap, ())
RuntimeError: can't start new thread
在使用 python + mongodb + celery 時,如果遇到了 RuntimeError,這可能是由於當前的 python 程序已經達到了操作系統允許的最大線程數量,因此無法再創建新的線程。在這種情況下,可以通過以下幾種方式來解決問題:
openai回答的真好,以後遇到問題我也要試試