Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/miyakogi/syncer
Async to sync converter
https://github.com/miyakogi/syncer
async asyncio python
Last synced: 6 days ago
JSON representation
Async to sync converter
- Host: GitHub
- URL: https://github.com/miyakogi/syncer
- Owner: miyakogi
- License: mit
- Created: 2016-02-23T15:27:48.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-11-08T23:02:48.000Z (about 1 year ago)
- Last Synced: 2024-10-29T22:56:39.682Z (14 days ago)
- Topics: async, asyncio, python
- Language: Python
- Size: 347 KB
- Stars: 74
- Watchers: 5
- Forks: 7
- Open Issues: 4
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
Syncer
======.. image:: https://img.shields.io/pypi/v/syncer.svg
:target: https://pypi.python.org/pypi/syncer.. image:: https://img.shields.io/pypi/pyversions/syncer.svg
:target: https://pypi.python.org/pypi/syncerSyncer is an async-to-sync converter for python.
* PyPI: https://pypi.python.org/pypi/syncer/
* Documentation: https://miyakogi.github.io/syncer/
* Source code: https://github.com/miyakogi/syncer/Features
========Sometimes (mainly in test) we need to convert asynchronous functions to normal,
synchronous functions and run them synchronously. It can be done by
``ayncio.get_event_loop().run_until_complete()``, but it's quite long...Syncer makes this conversion easy.
* Convert async-function (defined by ``aync def``) to normal (synchronous) function
* Evaluate coroutines synchronouslyInstall
=======At the command line::
$ pip install syncer
Usage
=====This module has only one function: ``syncer.sync``.
.. code-block:: py
from syncer import sync
async def async_fun():
...
return 1
b = sync(async_fun) # now b is synchronous
assert 1 == b()To test the above ``async_fun`` in asynchronous test functions:
.. code-block:: py
import unittest
class TestA(unittest.TestCase):
# ``sync`` can be used as decorator.
# The decorated function becomes synchronous.
@sync
async def test_async_fun(self):
self.assertEqual(await async_fun(), 1)Or, keep test functions synchronous and get results synchronously:
.. code-block:: py
class TestA(unittest.TestCase):
def test_async_fun(self):
# run coroutine and return the result
self.assertEqual(sync(async_fun()), 1)
# This is equivalent to below, just a shortcut
self.assertEqual(
asyncio.get_event_loop().run_until_complete(async_fun()), 1)More examples/use-cases will be found in `test `_.
License
=======`MIT license `_
Credits
=======This package was created with Cookiecutter_ and the `audreyr/cookiecutter-pypackage`_ project template.
.. _Cookiecutter: https://github.com/audreyr/cookiecutter
.. _`audreyr/cookiecutter-pypackage`: https://github.com/audreyr/cookiecutter-pypackage