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)

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

Uploaded Source

Built Distributions

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

Uploaded Source

syncer-1.0.0-py3.5.egg (3.5 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for syncer-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9c8db41cfc245ede61141e94e62b6a893eae78260b704f63a2f1bdb0d2ba0652
MD5 888c9ff903edc82fde6d2bfb9f0d8688
BLAKE2b-256 d9d4e33fb23c3f6c9b68f4052245d494535abb64565a2a3e50cb2d8489e895b0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for syncer-1.0.0.linux-x86_64.tar.gz
Algorithm Hash digest
SHA256 64194237bbd0f9f9a28e78a4e224a3df127d9208dee5b5d39d4087b2b4f0cc98
MD5 1a84cc0d713b41b6808114ec35c04853
BLAKE2b-256 dc02b3b805ba6959588638a42a48b5b20b9e59ea442c03a4821e677ff2e2d55d

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for syncer-1.0.0-py3.5.egg
Algorithm Hash digest
SHA256 24a6fb4147b43971d30d4d417e896906781743909a9306e9bf4cf9a4b00965b5
MD5 37b30f5f75c23b83206050fc14d9972d
BLAKE2b-256 61a8c9a32be16ff119d9bc77117ca0478d2b5e2a134ba7fd68480dcd1897b6a6

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