Python 2.4.3多线程更改并发/线程数并查找当前并发
我正在学习python中的多线程,并编写了这个脚本Python 2.4.3多线程更改并发/线程数并查找当前并发,python,multithreading,concurrency,centos,python-multithreading,Python,Multithreading,Concurrency,Centos,Python Multithreading,我正在学习python中的多线程,并编写了这个脚本 #!/usr/bin/python import threading import time arg = ["this is one", "this is two", "this is three", "this is four", "this is five"] def filestat(stat): print stat print threading.currentThread() time.sleep(5)
#!/usr/bin/python
import threading
import time
arg = ["this is one", "this is two", "this is three", "this is four", "this is five"]
def filestat(stat):
print stat
print threading.currentThread()
time.sleep(5)
print stat,threading.currentThread(),"after sleep"
thread = [threading.Thread(target=filestat, args=(arg[x],)) for x in range(len(arg)) ]
for i in thread:
i.start()
我运行了这个脚本,可以看到列表中的所有元素都是同时打印的,尽管有sleep5。此脚本中的当前并发级别是多少?它是否取决于列表中元素的数量?有什么方法可以提高/降低执行线程的并发性吗?您创建了5个不同的线程,并同时启动了它们,因此您当然可以同时获得所有结果。i、 start不会等待线程完成,它会启动当前线程,并移动到下一个迭代-同时每个启动的线程都在后台独立运行。@谢谢,如果我想将并发限制为2,我该怎么做?