Skip to main content

Async to sync converter

Project description

Syncer

https://img.shields.io/pypi/v/syncer.svg https://img.shields.io/pypi/pyversions/syncer.svg https://img.shields.io/travis/miyakogi/syncer.svg https://codecov.io/github/miyakogi/syncer/coverage.svg?branch=master

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

syncer-1.0.1.tar.gz (9.9 kB view details)

Uploaded Source

Built Distributions

syncer-1.0.1.linux-x86_64.tar.gz (3.0 kB view details)

Uploaded Source

syncer-1.0.1-py3.5.egg (3.4 kB view details)

Uploaded Source

File details

Details for the file syncer-1.0.1.tar.gz.

File metadata

  • Download URL: syncer-1.0.1.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for syncer-1.0.1.tar.gz
Algorithm Hash digest
SHA256 73c47c64c2c1e3f8506b98ad1d4173b37404430394ffdb9f96812d2eeff78490
MD5 8186b44d9a2335ac1e4b1a0c5243ad7e
BLAKE2b-256 98e48b84f534d241edeaff929dbfcf028a4f7c0aaae2ddee039f6a0b600f976a

See more details on using hashes here.

File details

Details for the file syncer-1.0.1.linux-x86_64.tar.gz.

File metadata

File hashes

Hashes for syncer-1.0.1.linux-x86_64.tar.gz
Algorithm Hash digest
SHA256 7996642a32310844d90d968aea9d4eba105c4c9519ecc1ad11fee57d13e22d85
MD5 7d0282fd1394b8fbd2bc9b40ab9d14ad
BLAKE2b-256 d9ac58ec9a900c4d30f0aae5f7ad06c5a8d6a8492179969ac0e94f8f444a1dd5

See more details on using hashes here.

File details

Details for the file syncer-1.0.1-py3.5.egg.

File metadata

  • Download URL: syncer-1.0.1-py3.5.egg
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for syncer-1.0.1-py3.5.egg
Algorithm Hash digest
SHA256 8e33446d89e5e731fe03c68691a9d908f3c05845656ac24b25f1480ae9aafe0f
MD5 13bc928c5b637943a952fe7662a23b9b
BLAKE2b-256 e3461c3581d5200495d193c9ce43a542b8d9c6531ecb9253a5756df5f41e3a54

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page