优化了代码结构

定义了两个函数设置两个进程,将进程之间通信的过程放在main函数中,执行模块简化,保持与之前代码一直的代码风格
pull/431/head
Zpadger 2020-01-10 17:11:02 +08:00 committed by GitHub
parent d77e71c167
commit a92e692852
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 13 deletions

View File

@ -8,25 +8,33 @@ Date: 2018-03-20
import multiprocessing
import os
def sub_task(queue):
print('子进程进程号:', os.getpid())
def sub_task1(queue):
print('子进程进程号:',os.getpid())
counter = 0
while counter < 1000:
while counter < 10:
queue.put('Pong')
counter += 1
if __name__ == '__main__':
print('当前进程号:', os.getpid())
queue = multiprocessing.Queue()
p = multiprocessing.Process(target=sub_task, args=(queue,))
p.start()
def sub_task2(queue):
counter = 0
while counter < 1000:
while counter < 10:
queue.put('Ping')
counter += 1
p.join()
def main():
print('当前进程号:', os.getpid())
queue = multiprocessing.Queue()
p1 = multiprocessing.Process(target=sub_task1, args=(queue,))
p1.start()
p1.join()
p2 = multiprocessing.Process(target=sub_task2, args=(queue,))
p2.start()
p2.join()
print('子任务已经完成.')
for _ in range(2000):
for _ in range(20):
print(queue.get(), end='')
if __name__ == '__main__':
main()