CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day16-20/code/example20.py
Views: 729
1
"""
2
线程间通信(共享数据)非常简单因为可以共享同一个进程的内存
3
进程间通信(共享数据)比较麻烦因为操作系统会保护分配给进程的内存
4
要实现多进程间的通信通常可以用系统管道、套接字、三方服务来实现
5
multiprocessing.Queue
6
守护线程 - daemon thread
7
守护进程 - firewalld / httpd / mysqld
8
在系统停机的时候不保留的进程 - 不会因为进程还没有执行结束而阻碍系统停止
9
"""
10
from threading import Thread
11
from time import sleep
12
13
14
def output(content):
15
while True:
16
print(content, end='')
17
18
19
def main():
20
Thread(target=output, args=('Ping', ), daemon=True).start()
21
Thread(target=output, args=('Pong', ), daemon=True).start()
22
sleep(5)
23
print('bye!')
24
25
26
if __name__ == '__main__':
27
main()
28
29