While I was working on a Python project, I needed asynchronous programming and noticed that I couldn't find a lightweight library to do so. So I made myself a promise...
from threading import Thread from threading import Event class Deferred(object): def __init__(self): self._event = Event() self._rejected = False self._result = None def resolve(self, value): self._rejected = False self._result = value self._event.set() def reject(self, reason): self._rejected = True self._result = reason self._event.set() def promise(self): promise = Promise(self) return promise class Promise(object): def __init__(self, deferred): self._deferred = deferred def then(self, resolved = None, rejected=None): defer = Deferred() def task(): try: self._deferred._event.wait() if self._deferred._rejected: result = self._deferred._result if rejected: result = rejected(self._deferred._result) defer.reject(result) else: result = self._deferred._result if resolved: result = resolved(self._deferred._result) defer.resolve(result) except Exception as ex: defer.reject(ex.message) Thread(target=task).start() return defer.promise() def wait(self): self._deferred._event.wait() @staticmethod def wait_all(*args): for promise in args: promise.wait()
------------------------------------------------------------------------------
And with that in place I can now write code like this:
------------------------------------------------------------------------------
controller = Controller("COM7") controller.start() message1 = DiscoveryRequest() promise1 = controller.send(message1).then(on_ready, on_error) message2 = RequestNodeInfoRequest(1) promise2 = controller.send(message1).then(on_ready, on_error) Promise.wait_all(promise1, promise2) controller.stop()
------------------------------------------------------------------------------