Home > Mobile >  A minimal reproducible example to show asynchronous behavior using callbacks in Python
A minimal reproducible example to show asynchronous behavior using callbacks in Python

Time:02-11

Background: To me it seems clear, that the conecpt of callbacks is flexible, but I also thought, it makes code much faster. However, the following example works, but it cannot show that time can be saved using a callback Ref1:

import time
from time import sleep

def callbackFunc(delay):
  time.sleep(delay)
  print("callback:     message 3 delay "   str(delay))

def saysomething(delay, callback):
  print("saysomething: message 2 delay "   str(delay))
  callback(delay) # hier muss ich schon wissen, dass nur "delay" benötigt wird...
  time.sleep(2)

if __name__ == '__main__':
  t0 = time.time()
  print("main:         message 1.")
  saysomething(2, callbackFunc)
  print("main:         message 4.")
  print("\ntime: ",time.time() - t0)

Output

main:         message 1.
saysomething: message 2 delay 2
callback:     message 3 delay 2
main:         message 4.
time:  4.01

So how can I achive something like this

main:         message 1.
saysomething: message 2 delay 2
callback:     message 3 delay 2
main:         message 4.
time:  2  !!!!!!!!!!!!!

Perhaps it was even possible to switch the order of messages 3 & 4? Or do I get anything wrong?


Perhaps, these answers here and here and the following code from here which does not use callbacks but shows asynchronous behavior help?

CodePudding user response:

This is irrelevant w.r.t. the SWIG callbacks used by the routing library.

CodePudding user response:

sleep() suspends execution of the calling thread, so you can't call it on the main thread if you want to show asynchronous behaviour.

You can use threads or the built-in event loop to show asynchronous behaviour.

Here's an example using the built-in event loop:

import asyncio
import time
# from time import sleep  # -


def callbackFunc(delay):
    # time.sleep(delay)                                                                               # -
    # print("callback:     message 3 delay "   str(delay))                                            # -
    asyncio.get_event_loop().call_later(delay, print, "callback:     message 3 delay "   str(delay))  #  


def saysomething(delay, callback):
    print("saysomething: message 2 delay "   str(delay))
    callback(delay)  # hier muss ich schon wissen, dass nur "delay" benötigt wird...
    # time.sleep(2)                                       # -
    asyncio.get_event_loop().call_later(2, lambda: None)  #  


def run_until_complete(loop):                                                             #  
    loop.call_soon(lambda: run_until_complete(loop) if loop._scheduled else loop.stop())  #  
    if not loop.is_running():                                                             #  
        loop.run_forever()                                                                #  


if __name__ == '__main__':
    t0 = time.time()
    print("main:         message 1.")
    saysomething(2, callbackFunc)
    print("main:         message 4.")
    run_until_complete(asyncio.get_event_loop())  #  
    print("\ntime: ", time.time() - t0)

Output:

main:         message 1.
saysomething: message 2 delay 2
main:         message 4.
callback:     message 3 delay 2

time:  2.000
  • Related