简体   繁体   English

如何增加 Python 多线程中的线程数?

[英]How to increase number of threads in Python multithreading?

I am developing a multithreads app using Python3.我正在使用 Python3 开发多线程应用程序。 I want to run over 100k threads at a time for backend server, but threads are created 10k in max.我想为后端服务器一次运行超过 100k 个线程,但最多创建 10k 个线程。 I think my server pc has enough resources for lots of threads.(cpu, ram).我认为我的服务器电脑有足够的资源来处理很多线程。(cpu、ram)。 How can I increase the number of threads in Python3 multithreading?如何增加 Python3 多线程中的线程数?

for i in range(0, 1000000):
    t = i.Thread(target=func, args=(i))
    t_list.append(t)

for t in t_list:
    t.start()

I'm going to argue that "you shouldn't do that".我要争辩说“你不应该那样做”。 It would be rare that you need that many threads, or that the system can handle it well.很少需要这么多线程,或者系统可以很好地处理它。 Threads are not a magic solution to deal with lots of incoming requests to process.线程并不是处理大量传入请求的神奇解决方案。

Instead, I suggest you create a limited number of threads in a "pool" and use a queue or similar mechanism to send tasks to the pool.相反,我建议您在“池”中创建有限数量的线程,并使用队列或类似机制将任务发送到池中。 See this answer for details on how to implement something like this.有关如何实现此类内容的详细信息,请参阅此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM