更新
This commit is contained in:
36
Python-100-Days/Day01-15/Day13/multiprocess1.py
Normal file
36
Python-100-Days/Day01-15/Day13/multiprocess1.py
Normal file
@@ -0,0 +1,36 @@
|
||||
"""
|
||||
|
||||
使用Process类创建多个进程
|
||||
|
||||
Version: 0.1
|
||||
Author: 骆昊
|
||||
Date: 2018-03-20
|
||||
|
||||
"""
|
||||
|
||||
# 通过下面程序的执行结果可以证实 父进程在创建子进程时复制了进程及其数据结构
|
||||
# 每个进程都有自己独立的内存空间 所以进程之间共享数据只能通过IPC的方式
|
||||
|
||||
|
||||
from multiprocessing import Process, Queue
|
||||
from time import sleep
|
||||
|
||||
|
||||
def sub_task(string, q):
|
||||
number = q.get()
|
||||
while number:
|
||||
print('%d: %s' % (number, string))
|
||||
sleep(0.001)
|
||||
number = q.get()
|
||||
|
||||
|
||||
def main():
|
||||
q = Queue(10)
|
||||
for number in range(1, 11):
|
||||
q.put(number)
|
||||
Process(target=sub_task, args=('Ping', q)).start()
|
||||
Process(target=sub_task, args=('Pong', q)).start()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user