Async to sync converter
Project description
Syncer
Syncer is an async-to-sync converter for python >= 3.5.
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 it 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
Or, if you have virtualenvwrapper installed:
$ mkvirtualenv syncer $ 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 synchronous, get result 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)
License
Credits
This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for syncer-0.1.0.linux-x86_64.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | d81cff3a2c5bf326e6baca379ccacba2a02bb0382b08c625c07584fd977d94b2 |
|
MD5 | 93af8cfbd8f37f894109dc1dd730b554 |
|
BLAKE2b-256 | 5c9c1fa5223d5d505de07fbbb090f0b3c927b4834b16d26be92cc65d257cad73 |