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

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 async-function (defined by aync def) to normal (synchronous) function

  • Evaluate coroutines synchronously

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-2.0.3.tar.gz (11.5 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: syncer-2.0.3.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for syncer-2.0.3.tar.gz
Algorithm Hash digest
SHA256 4340eb54b54368724a78c5c0763824470201804fe9180129daf3635cb500550f
MD5 4152a113474fc09e2c0bde4894f8b0e7
BLAKE2b-256 8dddd4dd75843692690d81f0a4b929212a1614b25d4896aa7c72f4c3546c7e3d

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