3

I would like to run a function asynchronously in Python, calling the function repeatedly at a fixed time interval. This java class has functionality similar to what I want. I was hoping for something in python like:

pool = multiprocessing.Pool()
pool.schedule(func, args, period)
# other code to do while that runs in the background
pool.close()
pool.join()

Are there any packages which provide similar functionality? I would prefer something simple and lightweight.

How could I implement this functionality in python?

This post is similar, but asks for an in process solution. I want a multiprocess async solution.

Community
  • 1
  • 1
user2133814
  • 2,431
  • 1
  • 24
  • 34

1 Answers1

0

Here is one possible solution. One caveat is that func needs to return faster than rate, else it wont be called as frequently as rate and if it ever gets quicker it will be scheduled faster than rate while it catches up. This approach seems like a lot of work, but then again parallel programming is often tough. I would appreciate a second look at the code to make sure I don't have a deadlock waiting somewhere.

import multiprocessing, time, math


def func():
    print('hello its now {}'.format(time.time()))


def wrapper(f, period, event):
    last = time.time() - period
    while True:
        now = time.time()

        # returns True if event is set, otherwise False after timeout
        if event.wait(timeout=(last + period - now)):
            break
        else:
            f()
            last += period


def main():
    period = 2
    # event is the poison pill, setting it breaks the infinite loop in wrapper
    event = multiprocessing.Event()
    process = multiprocessing.Process(target=wrapper, args=(func, period, event))
    process.start()

    # burn some cpu cycles, takes about 20 seconds on my machine
    x = 7
    for i in range(50000000):
        x = math.sqrt(x**2)

    event.set()
    process.join()
    print('x is {} by the way'.format(x))

if __name__ == '__main__':
    main()
user2133814
  • 2,431
  • 1
  • 24
  • 34