Giter Club home page Giter Club logo

syncer's Introduction

Syncer

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.

syncer's People

Contributors

miyakogi avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.