示例#1
0
 def __init__(self, loop=None):
     self._loop = loop if loop else asyncio.get_event_loop()
     self._beans = OrderedDict()
     self._label_index = {}
     self._finished = set()
     self._pending = set()
     self._coros = []
     self._bean_done_events = {}
     self._signal_dispatcher = Signal(loop=self._loop)
     self._stopped = self._started = False
     self._finished_event = asyncio.Event(loop=self._loop)
     self._stopping_event = asyncio.Event(loop=self._loop)
     self._logger = logging.getLogger(self.__class__.__name__)
示例#2
0
    async def callback6(self, **kwargs):
        print('callback6 was called')

    @classmethod
    def callback7(cls, **kwargs):
        print('callback7 was called')

    @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)),
示例#3
0
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()))
示例#4
0
class Mike(Somebody):
    pass


class Ashley(Somebody):
    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']))
示例#5
0
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())
示例#6
0
import asyncio
from asyncio_dispatch import Signal


async def callback(**kwargs):
    print('callback was called!')


async def connect_and_send_signal(signal, callback):
    await signal.connect(callback)
    print('sending the signal.')
    await signal.send()


loop = asyncio.get_event_loop()

# initialize the signal
signal = Signal(loop=loop)

# connect the callback to the signal and send the signal
loop.run_until_complete(connect_and_send_signal(signal, callback))
示例#7
0
class Mike(Somebody):
    pass


class Ashley(Somebody):
    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 = [
示例#8
0
class Mike(Somebody):
    pass


class Ashley(Somebody):
    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']))
示例#9
0

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'}))
示例#10
0
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"})
    )
)