README.md 2.4 KB

Asynchronous Events

build status coverage report

This library provides a simple event interface for threaded environments. It's primary aim is to keep processing of events within their respective threads. Although 'threads' in Python are mostly artificial, considering the GIL, this library is still beneficial.

  • handling exceptions within the correct thread
  • clear separation, and therefore understanding, of thread workloads
  • creating async code and loops without requiring asyncio spaghetti

Operation

Avents ties threads to object instances. Therefore threads should target the loop() function of an Abject. If implementing threads in an existing thread loop, then include a if self._handlEvents(): break call.

Any given event handler will be executed within an Abject's thread loop. Avents discovers this by using the instance of a method-function or the first parameter of the fire() function. If neither can be found, the handler is put on the _sub_main event stack that is executed in the run_forever() helper function.

Additional

This library also includes a barebones, async-less, Event class from which Avent inherits its core functionality.

Fluid Events

This is a slight modification of the Event class to allow any number of args for handlers and the fire() function without errors.

from avent import Fluid as Event

ev = Event()
ev += lambda a, b, c: print(a,b,c)
ev += lambda a: print(a)

ev.fire(1, 2)
# handler 1 => "1, 2, None"
# handler 2 => "1"

Strict Events

This is the polar opposite of a 'Fluid' event. Permissible arguments are specific on init and handlers and events will throw ArgsError whenever the rules are broken.

from avent import Strict as Event

ev = Event('a', 'b')

ev += lambda a,b,c: print(a,b,c) # => throws ArgsError
ev += lambda a: print(a)         # => throws ArgsError

ev.fire(1)       # => throws ArgsError
ev.fire(1, 2, 3) # => throws ArgsError

# acceptable
ev += lambda a,b: print(a,b)
ev.fire(1,2)

Authors

Legal

Copyright (c) 2017 MK2 Engineering Solutions Pty. Ltd. All Rights Reserved.