You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
miyakogi 6b2c182f8f Run tests on python3.7 4 years ago
docs Bump version: 1.2.2 → 1.3.0 5 years ago
tests Update setup.py 5 years ago
.coveragerc Use tox on travis 5 years ago
.gitignore Update gitignore 5 years ago
.travis.yml Run tests on python3.7 4 years ago
LICENSE First commit 7 years ago
MANIFEST.in First commit 7 years ago
README.rst Update info 7 years ago
mypy.ini Add type hints 5 years ago
requirements-dev.txt Use tox on travis 5 years ago
requirements-docs.txt Use tox on travis 5 years ago
requirements-test.txt Use tox on travis 5 years ago
setup.cfg Bump version: 1.2.2 → 1.3.0 5 years ago
setup.py Update setup.py 5 years ago
syncer.py Add type hints 5 years ago
tox.ini Run tests on python3.7 4 years ago

README.rst

Syncer

image

image

image

image

Syncer is an async-to-sync converter for python.

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

MIT license

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.