利用协程计算质数

利用协程计算质数

import time ,math
from gevent import monkey
monkey.patch_all()
import gevent
from gevent.queue import Queue
num = int(input('请输入一个数字:'))
aa = time.time()
num_list = []
for a in range(3,num+1) :
    num_list.append(a)
bb = time.time()
list_z=[2]
work = Queue()
for n in num_list :
    work.put_nowait(n)
def play() :
    while not work.empty() :
        m = work.get_nowait()
        for i in range(2,int(math.sqrt(m)+1)) :
            if m%i  == 0 :
                break
        else :
            list_z.append(m)
takes = []
for x in range(100) :
    take = gevent.spawn(play)
    takes.append(take)
gevent.joinall(takes)
cc = time.time()
print(bb-aa)
print(cc-aa)
上一篇:python 协程编程之gevent


下一篇:Flask部署