iT邦幫忙

0

RuntimeError: can't start new thread

  • 分享至 

  • xImage

小弟最近在使用 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

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
deh
iT邦研究生 1 級 ‧ 2022-12-07 10:44:30

在使用 python + mongodb + celery 時,如果遇到了 RuntimeError,這可能是由於當前的 python 程序已經達到了操作系統允許的最大線程數量,因此無法再創建新的線程。在這種情況下,可以通過以下幾種方式來解決問題:  

  • 將 python 程序中的線程數量限制在一個合理的數量。通過限制線程數量,可以避免出現這種問題。  
  • 檢查當前程序中是否有僵死或者無限循環的線程,如果有,可以通過終止這些線程來釋放資源。  
  • 如果當前的操作系統配置允許,可以增加操作系統的最大線程數量。  
  • 如果以上方法都無法解決問題,那麼可能需要考慮更換更強大的操作系統或者更換更高效的程序語言來解決問題。

openai回答的真好,以後遇到問題我也要試試

我要發表回答

立即登入回答