在 Python 中管理多个线程 [英] Manage Multiple Threads in Python
问题描述
函数 gen() 生成一个字符串,这是一个要传入 os.system() 的命令.
Function gen() generates a string, which is a command to be passed into os.system().
我需要同时启动 20 个线程,为每个线程调用 gen(),并在每个线程中运行 gen() 给我的命令.(平行)
I need to start 20 threads at the same time, call gen() for each thread, and run the command gen() gives me in each thread. (parallel)
一旦一个命令在线程中执行完毕,我想再次调用 gen() 以生成另一个命令,并在同一线程中运行该命令.
Once a command finishes executing in a thread, I want to call gen() again to generate another command, and run that command in the same thread.
我想一直这样做,直到 10 分钟过去.
I want to to keep doing this until 10 minutes have passed.
我该怎么做?
推荐答案
看看这个,希望这会有所帮助.
Take a look at this, hope this helps.
其实没那么难
但在处理使用相同变量的线程时请记住,使用 锁一个>!
but keep in mind when working with threads which use the same variable to use locks!
这篇关于在 Python 中管理多个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!