我已经在SO中查看了很多问题,所以我不认为这是重复的,因为大多数问题都是关于如何制作线程队列循环,我已经尝试过没有成功
问题:
我有一个线程队列,一次只能运行一个进程。
为此,我创建了一个永无止境的线程进程,其中包含while True
。
这个while True
基本上检查一个队列(简单列表)是否有任何项目,然后检索并运行它们,将它们从队列中删除。
一切都运行正常,除非这段代码产生了数千个开放线程并且永远不会关闭它们,直到我开始收到此错误:
文件" /usr/lib/python3.5/threading.py",第844行,开始
_start_new_thread(self._bootstrap,())
RuntimeError:无法启动新线程
从我的角度来看,我只启动一次线程,并处理方法调用。它不应该产生新的线程。
我做错了什么?
代码:
import threading
import queue
[...]
class Monitor:
[...]
"""
Updates this monitor's ping indicator
"""
def setLastMonitorPingIndicator(self):
current_timestamp = datetime.datetime.utcnow().strftime('%s')
if (self.last_ping != current_timestamp):
self.last_ping = current_timestamp
# Inserting into the indicator table
self.dbcursor.execute("""
INSERT INTO
indicator (balance_id, name, value)
VALUES
(NULL, %s, %s)
ON CONFLICT
(name) WHERE balance_id IS NULL
DO UPDATE SET
value = %s;""", ['last_balancemonitor_binance_ping', current_timestamp, current_timestamp])
"""
Adds processes to threaded queue
"""
def queueProcess(self, method, args=None):
self._debug(4, "Queueing threaded method '"+method.__name__+"' ("+str(self.threadQueue.qsize())+" processes awaiting | "+str(self.threaded_processes_running)+" running)")
self.threadQueue.put([method, args])
"""
Processes queued threaded processes
"""
def processThreadedQueue(self):
while True:
# wait for an item from the producer
item = self.threadQueue.get()
if item is None:
continue
self.threaded_processes_running = self.threaded_processes_running + 1
method = item[0]
now = time.time()
self._debug(4, "Running '"+method.__name__+"' from threaded queue.")
try:
if item[1] is None:
method()
else:
method(item[1])
except Exception:
print(traceback.format_exc())
self._debug(4, "Finished running '"+method.__name__+"' from threaded queue. Took "+str(round((time.time()-now),2))+" seconds.")
self.threaded_processes_running = self.threaded_processes_running - 1
"""
Main method
"""
def start(self):
worker = threading.Thread(target=self.processThreadedQueue)
worker.setDaemon(True) # Already tried with and without daemonizing it, same result
worker.start()
[...]
while (True):
[...]
# Updating this monitor's ping
self.queueProcess(self.setLastMonitorPingIndicator)
sleep(5)
monitor = Monitor()
monitor.start()