@staticmethod def callback8(**kwargs): print('callback8 was called') loop = asyncio.get_event_loop() # create the signal signal = Signal(loop=loop) # initialize the test class test = Test() tasks = [ # connect the function and coroutines loop.create_task(signal.connect(callback1)), loop.create_task(signal.connect(callback2)), loop.create_task(signal.connect(callback3)), # connect the class methods loop.create_task(signal.connect(test.callback4)), loop.create_task(signal.connect(test.callback5)), loop.create_task(signal.connect(test.callback6)), loop.create_task(signal.connect(Test.callback7)), loop.create_task(signal.connect(Test.callback8)), ] loop.run_until_complete(asyncio.wait(tasks)) # send the signal loop.run_until_complete(signal.send())
import asyncio from asyncio_dispatch import Signal @asyncio.coroutine def callback(**kwargs): print('callback was called!') loop = asyncio.get_event_loop() # create the signal signal = Signal(loop=loop) # connect the callback to the Signal - This is a coroutine! loop.run_until_complete(signal.connect(callback)) # send the signal - This is also a coroutine! print('sending the signal.') loop.run_until_complete(signal.send())
import asyncio from asyncio_dispatch import Signal @asyncio.coroutine def callback(**kwargs): print('callback was called!') loop = asyncio.get_event_loop() # create the signal signal = Signal(loop=loop) # connect the callback to the Signal - This is a coroutine! loop.run_until_complete(loop.create_task(signal.connect(callback))) # send the signal - This is also a coroutine! print('sending the signal.') loop.run_until_complete(loop.create_task(signal.send()))
mike = Mike() ashley = Ashley() loop = asyncio.get_event_loop() # create two signals SIGNAL1 = Signal(loop=loop, message=None) SIGNAL2 = Signal(loop=loop, message=None) connection_tasks = [ # connect the signals. Mike and Ashley are each connected using different keys and senders. # Their callbacks will only be executed if a Signal is sent with a matching key or sender. loop.create_task( SIGNAL1.connect(mike.message_received, sender=ashley, keys=['important', 'books'])), loop.create_task(SIGNAL2.connect(mike.message_received, keys=['logs'])), loop.create_task( SIGNAL1.connect(ashley.message_received, sender=mike, keys=['love-notes', 'music'])), loop.create_task(SIGNAL2.connect(ashley.message_received, keys=['alert'])) ] # ensure the connections are made before trying to send signals loop.run_until_complete(asyncio.wait(connection_tasks)) send_tasks = [ # Try to send the signals without senders or keys. # Nothing should happen as there are no matching senders or keys
pass mike = Mike() ashley = Ashley() loop = asyncio.get_event_loop() # create two signals SIGNAL1 = Signal(loop=loop, message=None) SIGNAL2 = Signal(loop=loop, message=None) connection_tasks = [ # connect the signals. Mike and Ashley are each connected using different keys and senders. # Their callbacks will only be executed if a Signal is sent with a matching key or sender. loop.create_task(SIGNAL1.connect(mike.message_received, sender=ashley, keys=["important", "books"])), loop.create_task(SIGNAL2.connect(mike.message_received, keys=["logs"])), loop.create_task(SIGNAL1.connect(ashley.message_received, sender=mike, keys=["love-notes", "music"])), loop.create_task(SIGNAL2.connect(ashley.message_received, keys=["alert"])), ] # ensure the connections are made before trying to send signals loop.run_until_complete(asyncio.wait(connection_tasks)) send_tasks = [ # Try to send the signals without senders or keys. # Nothing should happen as there are no matching senders or keys loop.create_task(SIGNAL1.send(message="nobody is listening")), loop.create_task(SIGNAL2.send(message="nobody is listening")), # Ashley sends Mike a message, and Mike replies. Matching by senders loop.create_task(SIGNAL1.send(sender=ashley, message="hello Mike!")),
pass mike = Mike() ashley = Ashley() loop = asyncio.get_event_loop() # create two signals SIGNAL1 = Signal(loop=loop, message=None) SIGNAL2 = Signal(loop=loop, message=None) # connect the signals. Mike and Ashley are each connected using different keys and senders. # Their callbacks will only be executed if a Signal is sent with a matching key or sender. loop.create_task(SIGNAL1.connect(mike.message_received, sender=ashley, keys=['important', 'books'])) loop.create_task(SIGNAL2.connect(mike.message_received, keys=['logs'])) loop.create_task(SIGNAL1.connect(ashley.message_received, sender=mike, keys=['love-notes', 'music'])) loop.create_task(SIGNAL2.connect(ashley.message_received, keys=['alert'])) # Try to send the signals without senders or keys. # Nothing should happen as there are no matching senders or keys loop.create_task(SIGNAL1.send(message='nobody is listening')) loop.create_task(SIGNAL2.send(message='nobody is listening'))
def callback(signal, senders, keys, my_kwarg, payload): print() print('-' * 50) if SIGNAL is signal: print('signals match as expected!') print('senders=', senders) print('keys=', keys) print('my_kwarg= {}'.format(my_kwarg)) print('payload= {}'.format(payload)) loop = asyncio.get_event_loop() # create the signal and define two custom keyword arguments: my_kwarg and payload SIGNAL = Signal(loop=loop, my_kwarg='default', payload={}) # connect the callback to the signal - this method is a coroutine loop.run_until_complete(SIGNAL.connect(callback)) # send the signal with default keyword arguments - this method is also a coroutine loop.run_until_complete(SIGNAL.send()) # send the signal again with new values for my_kwarg and payload loop.run_until_complete(SIGNAL.send(my_kwarg='changed with send', payload={'anything': 'a dict can hold!', 'really': 'powerfull'}))
def callback(signal, senders, keys, my_kwarg, payload): print() print("-" * 50) if SIGNAL is signal: print("signals match as expected!") print("senders=", senders) print("keys=", keys) print("my_kwarg= {}".format(my_kwarg)) print("payload= {}".format(payload)) loop = asyncio.get_event_loop() # create the signal and define two custom keyword arguments: my_kwarg and payload SIGNAL = Signal(loop=loop, my_kwarg="default", payload={}) # connect the callback to the signal - this method is a coroutine loop.run_until_complete(loop.create_task(SIGNAL.connect(callback))) # send the signal with default keyword arguments - this method is also a coroutine loop.run_until_complete(loop.create_task(SIGNAL.send())) # send the signal again with new values for my_kwarg and payload loop.run_until_complete( loop.create_task( SIGNAL.send(my_kwarg="changed with send", payload={"anything": "a dict can hold!", "really": "powerfull"}) ) )