Skip to content

Instantly share code, notes, and snippets.

@APN-Pucky
Created November 7, 2019 21:40
Show Gist options
  • Save APN-Pucky/b67e9e7eee22bd802aa2e5046625b9ca to your computer and use it in GitHub Desktop.
Save APN-Pucky/b67e9e7eee22bd802aa2e5046625b9ca to your computer and use it in GitHub Desktop.
from multiprocessing import Process,Queue
import time
def queued(q,f,*args,**kwargs):
q.put(f(*args,**kwargs))
def res(a):
return next(a)
def gen(f,*args,**kwargs):
q = Queue()
p = Process(target=queued,args=(q,f,*args),kwargs=kwargs)
yield p.start()
yield [q.get(),p.join()][0]
def calc(f,*args,**kwargs):
g=gen(f,*args,**kwargs)
next(g)
return g
############################# DEMO/TEST #####################################
def test(a):
print("start calc")
time.sleep(2)
print("done calc")
return a*a
a=calc(test,a=5)
print("long current calculation")
time.sleep(1)
print("long current calculation")
time.sleep(1)
print("long current calculation")
res(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment