gpt4 book ai didi

Python:无法启动新线程。 <100 个事件线程

转载 作者:太空狗 更新时间:2023-10-29 21:49:40 25 4
gpt4 key购买 nike

我收到以下错误:

----- Match 93028: ------ Patch 5.11 ------78 Threads Active
----- Match 93029: ------ Patch 5.11 ------77 Threads Active
----- Match 93030: ------ Patch 5.11 ------76 Threads Active
----- Match 93031: ------ Patch 5.11 ------71 Threads Active
----- Match 93032: ------ Patch 5.11 ------55 Threads Active
----- Match 93033: ------ Patch 5.11 ------56 Threads Active
----- Match 93034: ------ Patch 5.11 ------57 Threads Active
----- Match 93035: ------ Patch 5.11 ------58 Threads Active
----- Match 93036: ------ Patch 5.11 ------59 Threads Active
Traceback (most recent call last):
File "pulldata.py", line 91, in <module>
getPatchData('5.11', '511')
File "pulldata.py", line 64, in getPatchData
matchThread.start()
File "/usr/lib/python3.4/threading.py", line 850, in start
_start_new_thread(self._bootstrap, ())
RuntimeError: can't start new thread

通常这是由于打开的线程过多造成的,但如您所见,我还打印了事件线程的数量。有 <100 个事件线程,所以我不确定问题出在哪里。相关代码如下:

slot = threading.BoundedSemaphore(value=1000)
def getMatchData(index,match,patch):
global requestsSent
global logfile
print("----- Match {0}: ------ Patch {1} ------{2} Threads Active".format(index,patch,threading.active_count()))
logfile.write("Parsing Match {0} for patch {1}:\n".format(index,patch))

#match is a class. get is a function that sends a request to the server and returns a request object from where I get the json response.
data = match.get().json()

#processdata

slot.release()

def getPatchData(patch, name):
global logfile
threads = []
matches = getAllMatches(patch)
for index, match in enumerate(matches):
slot.acquire()
matchThread = threading.Thread(target=getMatchData, args=(index,match,patch))
threads.append(matchThread)
matchThread.start()
for t in threads:
if not t.isAlive():
threads.remove(t)

for t in threads:
t.join()

插槽信号量应该限制事件线程的数量,但我认为无论如何我都没有达到 1000 个线程。在我假设此错误是由于我的线程数组指向线程引起的之前,我添加了代码以在它们不再事件时将它们从数组中删除。

我不明白为什么只有 59 个事件线程时我无法启动新线程。

此外,是否有更好的方法来实现我想要做的事情?每个线程向 API 发送一个请求。我尝试在没有并发的情况下这样做,但我什至没有接近我的速率限制。

最佳答案

我遇到了类似的问题,下面是我如何解决的。

不确定 OP 使用的是什么操作系统,但在 Linux 上,每个用户的进程数通常有限制。你可以用 ulimit -u 查看它(或者也是 ulimit -a )。该定义有点用词不当,因为限制实际上是在操作系统线程数(或 LWP)上。(请参阅已接受的答案:https://superuser.com/questions/376532/does-gnu-linux-counts-processes-and-threads-together-when-i-limit-their-number)

在我的系统上,限制似乎设置为 400(但它可以由管理员更改)。

您可以使用以下命令查看所有线程的列表:

ps -fLu <your_username>

在我的例子中,我的 python 应用程序会引发与 OP 报告的相同的异常,但 threading.active_count() 会返回 7。

原来我有很多以前 session 遗留下来的进程(我对 nohup 有点太热衷了​​......),每个都有几个线程,在系统中徘徊。删除它们可以消除线程创建错误。

关于Python:无法启动新线程。 <100 个事件线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32283003/

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