1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
| import threading
from time import *
def loop0(a, b, c):
print("参数:", a, b, c)
print("start loop0 at:", ctime())
sleep(4)
print("loop0 done at:", ctime())
def loop1():
print("start loop1 at:", ctime())
print("loop1 done at:", ctime())
def main():
print("starting at :", ctime())
# 线程中传递参数有三种方法
# 1.使用元组传递 threading.Thread(target=fun_name,args=(参数。。。))
# thread_1 = threading.Thread(target=loop0, args=(10, 21, 22))
# 2.使用字典传递 threading.Thread(target=fun_name,kwargs={"参数名": "参数值"....})
# thread_1 = threading.Thread(target=loop0, kwargs={"a": 10, "b": 21, "c": 22})
# 3.混合使用元组和字典传递 threading.Thread(target=fun_name,args=(10, 21, 22), kwargs={"参数名": "参数值"....})
thread_1 = threading.Thread(target=loop0, args=(10, 21), kwargs={"c": 22})
thread_2 = threading.Thread(target=loop1)
thread_1.start()
thread_2.start()
print("all done at:", ctime())
if __name__ == "__main__":
main()
out:
starting at : Wed Nov 13 15:00:00 2019
参数: 10 21 22
start loop0 at: Wed Nov 13 15:00:00 2019
start loop1 at: Wed Nov 13 15:00:00 2019
loop1 done at: Wed Nov 13 15:00:00 2019
all done at: Wed Nov 13 15:00:00 2019
loop0 done at: Wed Nov 13 15:00:04 2019
|