Python3.4 关于创建多线程的探索

Python3.4 A quest about create mul-threads

看这段代码:

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

也许你已经知道我的 puzzle.I 通过 "threading.Thread" 函数创建线程,它不是 运行 synchronously.Is 它的全局函数只有一个线程 运行一次?我用python3.4

threading.Thread(w("a")).start()

表示执行 w("a") 并将结果传递给 threading.Thread() 构造函数。您不是传递可调用对象而是调用它。您需要将函数及其参数分开:

threading.Thread(target = w, args = ["a"]).start()