README.rst 1.9 KB
Newer Older
1 2 3
Call
====

Nathan Graule's avatar
Nathan Graule committed
4 5
.. image:: https://api.codacy.com/project/badge/Grade/91959f98ff34469884415e96ba2ff763    :target: https://www.codacy.com/app/solarliner/call?utm_source=gitlab.com&utm_medium=referral&utm_content=solarliner/call&utm_campaign=Badge_Grade

6 7 8 9 10 11
Thread-based, JS-like asynchronous calls for Python. Works in both
Python 2.7 and Python 3.5+.

Install
-------

12 13 14 15 16 17
Release version:

.. code:: bash
	pip install call

Development version
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

.. code:: bash

    git clone https://gitlab.com/solarliner/call.git
    cd call
    # Activate virtualenv if needed
    python setup.py install

The library requires no other dependencies, and (will soon) support
Python's ``await`` keyword.

Use
---

Create a call:

.. code:: python

    def cb(resolve, reject):
        result = factorial(100)
        resolve(result)
        
    call = Call(cb)

42 43 44 45 46
Wrap a synchronous function in a Call:

.. code:: python
    call = Call.from_function(factorial, 10)

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
Chain calls with the ``then`` keyword

.. code:: python

    call = Call(cb).then(lambda val: print(val))

Catch errors:

.. code:: python

    call = Call(cb)\
        .then(lambda val: raise Exception())\
        .catch(lambda err: print('Whoops'))

Compose calls:

.. code:: python

    results = Call.all([Call(cb) for _ in range(10)])

Block thread until resolved (or raises on failure):

.. code:: python

    result = call.wait()

73 74
Wait for call to either resolve or reject. Note that it is not recommended to get the data directly, as it may be
``None``, which may or may not indicate that an error has occurred.
75 76 77 78 79

.. code:: python

    call.join()
    result = call.data  # Not recommended
Nathan Graule's avatar
Nathan Graule committed
80 81 82 83 84 85 86 87

Contribute
----------

The repository follows the ``git flow`` standards. Create a feature branch, then ask for a pull/merge request.

The main repository is on GitLab, however the GitHub mirror is functional and you should be able to ask for pull
requests. However, they will be processed in GitLab.