0

I need to implement an asyncronous timer to 'watch' the execution of a list of functions, till the timer expires. But the problem is function execution is a blocking call and in that case how can I track the timer if the function take too long to comeback.

functions = [func_1, func_2, func_3, func_n]

timer = Timer(30) # timer of 30 sec, just for example.
while timer.expires():
    for func in functions:
        func() # what if this function runs for a min

I would like to avoid multithreading and multiprocessing as far as possible, but if multiprocessing/threading is the only way out then please provide those solutions also.

What are different ways in python in which asynchronous behaviour can be achieved.

2
  • To clarify, does each function get a full 30 seconds to run, or do you have 30 seconds to make it through the whole list? Commented Jul 14, 2015 at 13:58
  • The timer duration applies for all the functions in the list put together. Commented Jul 14, 2015 at 19:13

1 Answer 1

1

If the functions you call are blocking due to IO, you can use the asyncio module to turn them into non blocking. At that point you wrap them into a future and set a timeout for their completion. Keep in mind that the timeout is considering only the IO.

If the functions are blocking due to CPU bound jobs (while loops, long calculations) there is no way to achieve that without using processes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.