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 >= 3.5.

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

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

Uploaded Source

Built Distributions

syncer-0.1.0.linux-x86_64.tar.gz (3.1 kB view details)

Uploaded Source

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

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for syncer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0b1382546d9f5ecce058e1cbd699cecbac0d2fe464c6e9b6416c37970056160c
MD5 6f2e28463c2ef0b600e57c1f5c65a004
BLAKE2b-256 fc80ef9dff442f05d5f9d5de8adeef163b8c58697b7626b82e880e312bc110e8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for syncer-0.1.0.linux-x86_64.tar.gz
Algorithm Hash digest
SHA256 d81cff3a2c5bf326e6baca379ccacba2a02bb0382b08c625c07584fd977d94b2
MD5 93af8cfbd8f37f894109dc1dd730b554
BLAKE2b-256 5c9c1fa5223d5d505de07fbbb090f0b3c927b4834b16d26be92cc65d257cad73

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for syncer-0.1.0-py3.5.egg
Algorithm Hash digest
SHA256 689e88df96f528c13e7ac0875423b64996656286d04c15ae9a9403cf18fba851
MD5 1cd16ae97c701af22aa04a2c9f918c32
BLAKE2b-256 53983b8d8d87b075c6a76a1172f1b609ba3b0a0ef67bdd5eee24049fd589c6a2

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