如何利用python中的多进程实现对10000的10000次幂的计算?
0 1530
0
该提问暂无详细描述
收藏
2021-04-15 09:07 更新 阿托 •  17079
共 1 个回答
高赞 时间
0
from multiprocessing import Process,Queue
import os,time,random

def write(q):
    print('Process to write:%s'%os.getpid())
    for value in ['10000']:
        print('Put %s to queue...'%value)
        q.put(value)
        time.sleep(random.random())

def read(q):
    print('Process to read:%s'%os.getpid())
    while True:
        value=q.get(True)
        print('Get %s from queue.'%(10000**10000))

if __name__=='__main__':
    q=Queue()
    pw=Process(target=write,args=(q,))
    pr=Process(target=read,args=(q,))
    pw.start()
    pr.start()
    if(q.empty()):
        print('Queue is empty.')
    else:
        pass
    pw.join()
    pr.join()
    pr.terminate

运行结果:

收藏
2021-04-15 09:08 更新 正直的烤面包 •  4006