Python 线程模块。不确定我是否创建了多个线程
我正在尝试学习“线程”模块。但是,我不确定我是否能够创建多个线程Python 线程模块。不确定我是否创建了多个线程,python,python-multithreading,Python,Python Multithreading,我正在尝试学习“线程”模块。但是,我不确定我是否能够创建多个线程 import threading import time def somefunction(): for loop in range (10): print "thread sleeps for 20 seconds" time.sleep(20) print "thread %d woke up" counter = 0 while counter < 10:
import threading
import time
def somefunction():
for loop in range (10):
print "thread sleeps for 20 seconds"
time.sleep(20)
print "thread %d woke up"
counter = 0
while counter < 10:
threader = threading.Thread(target=somefunction())
counter = counter +1
我做错了什么?您必须使用参数
target
中对somefunction
的引用,而不是调用该函数
import threading
import time
def somefunction():
for loop in range (10):
print "thread sleeps for 20 seconds"
time.sleep(20)
print "thread %d woke up"
for counter in range(10):
threader = threading.Thread(target=somefunction)
非常感谢。它起作用了。不过,我有一个问题。为了关闭所有线程,我可以在代码末尾添加这个?threader.join()
import threading
import time
def somefunction():
for loop in range (10):
print "thread sleeps for 20 seconds"
time.sleep(20)
print "thread %d woke up"
for counter in range(10):
threader = threading.Thread(target=somefunction)