Python and multiprocessing ... how to call a function in the main process?

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)

    ### HERE I WANT TO CALL runCallback IN THE MAIN PROCESS ###

    # This does not work... It calls runCallback in the separate process:
    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)
### some code that gets run before aFunc is called ###
### aFunc runs 10s later ###

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 ???

+3
source share
2 answers

, multiprocessing.

​​ , threading.Timer.

threading.Timer(10, aFunc).start()

Timer .cancel(), :

t = threading.Timer(10, runCallback, args=[uniqueId, funcs])
t.start()
timers.append((t, uniqueId))
# do other stuff
# ...
# run callbacks right now
for t, uniqueId in timers:
    t.cancel() # after this the `runCallback()` won't be called by Timer()
               # if it not been called already
    runCallback(uniqueId, funcs)

runCallback() :

def runCallback(uniqueId, funcs):
    f = funcs.pop(uniqueId, None) # GIL protects this code with some caveats
    if f is not None:
       f()
+4

, , ( , , ), signal, , , , (IPC), pipe .

, . lock funcs dict.

+3
source

Source: https://habr.com/ru/post/1796590/


All Articles