Async to sync converter
Project description
Syncer
Syncer 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 coroutine-function (defined by aync def) to normal (synchronous) function
- Run coroutines synchronously
- Support both async def and decorator (@asyncio.coroutine) style
Install
At the command line:
$ pip install syncer
Usage
This module has only one function: syncer.sync.
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:
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:
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
Credits
This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Filename, size | File type | Python version | Upload date | Hashes |
---|---|---|---|---|
Filename, size syncer-1.0.2.linux-x86_64.tar.gz (3.0 kB) | File type Dumb Binary | Python version any | Upload date | Hashes View |
Filename, size syncer-1.0.2-py3.5.egg (3.4 kB) | File type Egg | Python version 3.5 | Upload date | Hashes View |
Filename, size syncer-1.0.2.tar.gz (9.9 kB) | File type Source | Python version None | Upload date | Hashes View |
Hashes for syncer-1.0.2.linux-x86_64.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96b30579491700964356be9153504ccdeda09a123673553b4660872d72516c45 |
|
MD5 | c8a007446e154e794b565a8e3ff30b2e |
|
BLAKE2-256 | 2c206ddbb609db6f990bcea86c47d1082737dac09b62be5703ee2e1ba41c2bcd |