I would like to implement an async callback style function in python ... This is what I came up with, but I'm not sure how to really return to the main process and call the function.
funcs = {}
def runCallback(uniqueId):
'''
I want this to be run in the main process.
'''
funcs[uniqueId]()
def someFunc(delay, uniqueId):
'''
This function runs in a seperate process and just sleeps.
'''
time.sleep(delay)
runCallback(uniqueId)
def setupCallback(func, delay):
uniqueId = id(func)
funcs[uniqueId] = func
proc = multiprocessing.Process(target=func, args=(delay, uniqueId))
proc.start()
return unqiueId
This is how I want it to work:
def aFunc():
return None
setupCallback(aFunc, 10)
There is something here because I want it to be a little more complicated. Basically, when the code in the main process executes ... I want to examine the funcs dict and then run any of the callbacks that are not already running. This means that runCallback should also delete entries from the funcs dict ... funcs dict is not shared with individual processes, so I think runCallback needs to be called in the main process ???
source
share