Skip to content

Instantly share code, notes, and snippets.

@lanfon72
Created June 11, 2017 14:13
Show Gist options
  • Select an option

  • Save lanfon72/b50c2beef90c97366db5373e76646eab to your computer and use it in GitHub Desktop.

Select an option

Save lanfon72/b50c2beef90c97366db5373e76646eab to your computer and use it in GitHub Desktop.
import time
import asyncio
from concurrent import futures
def primes(num):
rv = []
p = [0] * num
k, n = 0, 2
print("start time:", time.time())
while k < num:
i = 0
while i < k and n % p[i] != 0:
i += 1
if i == k:
p[k] = n
k += 1
rv.append(n)
n += 1
print("end time:", time.time())
return rv
def main():
loop = asyncio.get_event_loop()
executor = futures.ProcessPoolExecutor()
start = time.time()
futs = []
for i in range(3):
futs.append(loop.run_in_executor(executor, primes, 5000))
done = loop.run_until_complete(asyncio.wait(futs))
end = time.time()
print("processing time:", end - start)
print(done)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment