Skip to main content

An easy async decorators and promises.

Project description

EAsync – easy async decorator and promises


Decorator around the functions for them to be asynchronous.

Used as a simple decorator or along with named arguments. Preserves function’s or method’s docs, names and other stuff using functools.wraps.

When the wrapped function is called, it produces the Promise of itself.


>>> from easync import async
>>> @async
>>> def f1():
>>>    # some heavy-weight code
>>> @async(daemon=True, print_exception=False)
>>> def f2():
>>>     # some faulty daemon we don't really care about
>>> @async(print_exception=logging.WARNING)
>>> def f3():
>>>     # setting another logging level
>>> f1().wait()
>>> f2()  # started daemon
>>> f3().then(callback, error)  # notice, here the print_exception is suppressed (see Promise doc for more)
param function:

The function to be decorated.

param Boolean daemon:

Optional. Create the daemon thread, that will die when no other threads left.

param print_exception:

Log level to log the exception, if any, or None to mute it. See logging.

param no_promise:

Will not return a thing, instead of returning Promise, good for decorating, for example, Tornado handlers.


Wrapped function, Promise generator.


This is a threading wrapper that performs asynchronous call of the provided function. The behaviour is inspired by JavaScript Promises, but differs in several points.

First, the resolution is based upon the return of the function beneath. On successful return, the result is stored in Promise.result On exception, exception is stored in Promise.exception More about how Promise resolves in Resolving Promises.

You can add callbacks by using methods Promise.then or Promise.catch They will create a new Promise resolving in either the resolution of the previous one or the underlying callback.

If at the final stage no exception is processed or were generated new ones, they will be printed.

Basic usage:

>>> from easync import Promise
>>> promise = Promise(func)(...args)
>>> promise.wait()
<result>  # if any
>>> promise.result
<result>  # if any
>>> promise.exception
Exception  # if something went wrong
>>> promise.exc_info
Tuple (Exception.__class__, Exception, traceback)  # if any

Callbacks usage:

>>> def callback(result):
>>>     do_stuff(result)
>>> def error(exception, exc_info):
>>>     my_log(exception, exc_info=exc_info)
>>> def some_final(_):
>>>     cleanup()
>>> Promise(func)(...args).then(callback, error).then(some_final)

The to-be-runned function.


This parameter holds the result the underlying function returned.


This parameter holds the exception if the underlying function fails.


This parameter holds the exception information tuple if the underlying function fails and the tuple was recovered. In case of Promise.reject, it will be None.


True if the function resolves. More about it in Resolving Promises.


threading.Event, sets when thread is started.


threading.Event, sets when thread is finished, no matter where had it been resolved or not.


Enables exception printout if caught. Uses logging to do so and has to be one of logging levels. False or None disable. Also automatically sets to False when a dependent Promise created through Promise.then or Promise.catch.

Resolving Promises


If the Promise is constructed with a function, it will resolve in it’s return or reject with the caught exception.

Other Promises

Just resolves in the same way the other one is resolved. Printouts will be suppressed in the first one.

Events and Conditions

If Promise is based on threading.Event or threading.Condition, it is resolved when the underlying Event or Condition occurs. The type testing is duck-type for having the wait method, so anything using the interface of waiting can be resolved, for example other Promises, or threads.

The resolving is based on testing is_failed on the object, and if that one returns, the Promise rejects. Otherwise, the get_result is called to obtain the result. Both are duck-type thingeys.

Anything else

Resolves successfully with the result equals to the passed-in argument.


__init__(function[, daemon=False, print_exception=logging.ERROR])

The constructor creates a threading.Thread wrapping the function. To start it, call the resulting object as a function with it’s arguments. (Explained in Promise.__call__)

>>> promise = Promise(func, print_exception=None)
>>> promise()
param function:

Function, Event, Condition, or anything else to resolve.

param daemon:

Sets up daemon flag in the thread. May be set later. Optional.

param print_exception:

Sets up the final exception printing level. Pass False to suppress.


__call__(*args, **kwargs)

Starts the thread and passes the arguments of the function into it. Returns self, for simple adding Promise.then, Promise.wait or Promise.catch.



Pauses the current thread to wait until the underlying promise resolves.

If timeout is set, raises easync.TimeoutError if it’s reached.

Returns result of the underlying function if there’s any.


then([resolved=None, rejected=None, print_exception=Promise.print_exception])

This method sets callbacks for a Promise.

NOTE this method suppresses the Promise default error handling by setting Promise.print_exception to False. You can then re-enable printouts manually, overriding the Promise.print_exception yourself.

NOTE calling this method twice on the same Promise object will result in duplicated exception printouts unless changed.

The result is a new Promise which resolves in:

callback exception:

If the called callback (either resolved or rejected) failed or raised anything.


If the underlying Promise rejected and no rejected callback was passed.

callback return:

The result of the called callback.


The result of the underlying Promise if it resolves and no resolved callback was passed.

This is done to have this kind of behaviour:

>>> Promise(action)(...args).then(parse_result).then(parse_one_more_result).catch(any_exception).then(cleanup)
function resolved(result):

The positive callback for the Promise. Has to accept one positional argument - the result.

function rejected(exception, exc_info):

The negative callback for the Promise. Has to accept two positional arguments - the caught exception and it’s exc_info.


Passed into the corresponding argument of the newly created Promise.


New Promise.


catch([callback=None, print_exception=Promise.print_exception])

The same as Promise.then (resolved=None, callback, print_exception).

Promise static methods



Resolves thing, regardless of what it is, to result.

param thing:



resolved Promise with the Promise.result equals to thing.



Rejects thing, regardless of what it is.

NOTE, if this will be called, the Promise.exc_info will be set None.

param thing:



rejected Promise with the Promise.exception equals to thing.



Resolves when all the items in the enumerate(things) are resolved. Or rejects when any of the items is rejected.

param things:

list of things or anything to be enumerate’d.


list of results of all the Promises for each of the items.


first caught exception.



Resolves when any of the items in the enumerate(things) is resolved. Or rejects when any of the items is rejected.

param things:

list of things or anything to be enumerate’d.


the result of the first resolved item.


first caught exception.

Other functions



Returns the first found attribute of result or success of the object obj, if any. Otherwise returns None.




found property:

if one of error, exception, failure is found.


if one of failed or is_failed is true.


if success is present and is False.



Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

easync-1.2.0b2.tar.gz (12.5 kB view hashes)

Uploaded Source

Built Distribution

easync-1.2.0b2-py2.py3-none-any.whl (15.2 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page