并发编程

1.开启线程的两种方式:
  进程,线程:
   进程只是用来把资源集中到一起(进程只是一个资源单位,或者说资源集合)而线程才是cpu上的执行单位)
   1.同一个进程内的多个线程共享该进程内的地址资源
  2.创建线程的开销远小于创建进程的开销(创建一个进程,就是创建一个车间,涉及到申请空间,
  而且在该空间内建至少一条流水线,但创建线程,就只是在一个车间内造一条流水线,无需申请空间,所以创建开销小)
 1 # 方式1:
 2 from threading import Thread
 3 import time
 4 
 5 def sayhi(name):
 6     time.sleep(2)
 7     print('%s say hello'%name)
 8 
 9 if __name__ == "__main__":
10     t=Thread(target=sayhi,args=('alice',))
11     t.start()
12     print('主线程')  # 一个进程,一个主线程,开启了一个线程
13 
14 # 方式2:
15 from threading import Thread
16 import time
17 
18 class Sayhi(Thread):
19     def __init__(self,name):
20         super().__init__()
21         self.name = name
22 
23     def run(self):
24         time.sleep(2)
25         print('%s say hello'%self.name)
26 
27 if __name__ == "__main__":
28     t = Sayhi('alice')
29     t.start()
30     print('主线程')
2.进程与线程的区别:
  1.开进程的开销远大于开线程
  2.同一进程内的多个线程共享该进程的地址空间
  3.瞅一眼pid
 1 # 1.
 2 import time
 3 from threading import Thread
 4 from multiprocessing import Process
 5 
 6 def piao(name):
 7     print('%s piaoing' %name)
 8     time.sleep(2)
 9     print('%s piao end' %name)
10 
11 if __name__ == '__main__':
12     # p1=Process(target=piao,args=('egon',))
13     # p1.start() # 发出去后,没有立刻反应 开进程开销大
14 
15     t1=Thread(target=piao,args=('egon',))
16     t1.start()  # 发出去后,立刻就反应了,开线程开销小
17 
18     print('主线程')
19 """
20 主线程
21 egon piaoing
22 egon piao end
23 """
24 """
25 egon piaoing
26 主线程
27 egon piao end
28 """
29 # 2.
30 from threading import Thread
31 from multiprocessing import Process
32 
33 n=100
34 def task():
35     global n
36     n=0
37 
38 if __name__ == '__main__':
39     # p1=Process(target=task)
40     # p1.start()
41     # p1.join() # 彼此隔离的 n = 100
42 
43     t1=Thread(target=task)
44     t1.start()
45     t1.join()  # 共享地址空间 n = 0
46 
47     print('主线程',n)
48 """
49 主线程 100 
50 """
51 """
52 主线程 0 
53 """
54 # 3.
55 from threading import Thread
56 from multiprocessing import Process,current_process
57 import os
58 
59 def task():
60    print(current_process().pid)
61    print(os.getpid(),os.getppid())
62 
63 if __name__ == '__main__':
64     # p1=Process(target=task)
65     # p1.start()  # pid 不一样
66     # p1.join()
67 
68     t1=Thread(target=task)
69     t1.start()  # 线程pid 一样
70     t1.join()
71 
72     print('主线程',current_process().pid)
73     print('主线程',os.getpid(),os.getppid())
74 """
75 38780
76 38780 43168
77 主线程 43168
78 主线程 43168 12480
79 """
80 """
81 58912
82 58912 12480
83 主线程 58912
84 主线程 58912 12480
85 """
3.Thread对象的其他属性或方法:
  currentThread().getName()
  t.setName()
  t.is_alive()
  t.isAlive()
  t.join()
  active_count()
  enumerate()
 1 from threading import Thread,currentThread,active_count,enumerate
 2 import time
 3 
 4 def task():
 5     print('%s is running'%currentThread().getName())
 6     time.sleep(2)
 7     print('%s is done'%currentThread().getName())
 8 
 9 if __name__ == "__main__":
10     t = Thread(target=task,name='子线程1')
11     t.start()
12     # t.join()
13     # print(t.getName())
14     # t.setName('儿子线程1')
15     # currentThread().setName('主进程')
16     # print(t.is_alive())
17     # print(t.isAlive())
18     # print('主线程:',currentThread().getName())
19     t.join()
20     # print(active_count())  # 2 没有join 1 有join
21     print(enumerate())  # [<_MainThread(MainThread, started 41144)>]
22 
23 
24 """
25 Thread-1 is running
26 Thread-1
27 主线程 MainThread
28 Thread-1 is done
29 """
30 """
31 子线程1 is running
32 子线程1
33 主线程 MainThread
34 子线程1 is done
35 """
36 """
37 子线程1 is running
38 主线程 MainThread
39 儿子线程1 is done
40 """
41 """
42 子线程1 is running
43 主线程: 主进程
44 子线程1 is done
45 """
46 """
47 子线程1 is running
48 子线程1 is done
49 False
50 False
51 主线程: MainThread
52 """
4.守护线程:
  一个进程什么时候被销毁,
1.不开线程,代码完毕,该进程就被销毁
2.主线程等到所有子线程死了后,他才死
守护线程等到所有的子线程死了后,他才死
 1 from threading import Thread
 2 import time
 3 
 4 def sayhi(name):
 5     time.sleep(2)
 6     print('%s say hello' %name)
 7 
 8 if __name__ == '__main__':
 9     t=Thread(target=sayhi,args=('egon',))
10     # t.setDaemon(True) #必须在t.start()之前设置
11     t.daemon=True
12     t.start()
13 
14     print('主线程')
15     print(t.is_alive())  # t 跟着死了 一个线程内没有开启非守护线程
16 """
17 主线程
18 True
19 """
20 
21 from threading import Thread
22 import time
23 
24 def foo():
25     print(123)
26     time.sleep(4)
27     print("end123")
28 
29 def bar():
30     print(456)
31     time.sleep(3)
32     print("end456")
33 
34 if __name__ == '__main__':
35     t1=Thread(target=foo)
36     t2=Thread(target=bar)
37 
38     t1.daemon=True
39     t1.start()
40     t2.start()
41     print("main-------")
42 """
43 123    foo time.sleep(1)
44 456
45 main-------
46 end123
47 end456
48 """
49 """
50 123     foo time.sleep(4)
51 456
52 main-------
53 end456
54 """
原文地址:https://www.cnblogs.com/mumupa0824/p/9396562.html