看看这段代码:
def w(i):
print("%s start" % i)
time.sleep(10)
print("end %s waiting" % i)
class ss(threading.Thread):
def __init__(self, i):
threading.Thread.__init__(self)
self.i = i
def run(self):
print("%s start" % self.i)
time.sleep(10)
print("end %s waiting" % self.i)
c=ss("c")
c.start()
d=ss("d")
d.start()
threading.Thread(w("a")).start()
threading.Thread(w("b")).start()
结果如下:
c start
a start
d start
end c waiting
end a waiting
end d waiting
b start
end b waiting
也许你已经知道了我的难题。我通过“threading.Thread”函数创建线程,它不是同步运行的。它是全局函数只有一个线程运行一次吗?我使用python3.4
答案 0 :(得分:1)
w("a")
表示执行threading.Thread()
并将结果传递给threading.Thread(target = w, args = ["a"]).start()
构造函数。而不是传递一个可调用的对象,而不是调用它。你需要将函数和它的参数分开:
{{1}}