Created
November 7, 2019 21:40
-
-
Save APN-Pucky/b67e9e7eee22bd802aa2e5046625b9ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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