Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

syncer

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

syncer

Async to sync converter

  • 2.0.3
  • PyPI
  • Socket score

Maintainers
1

Syncer

.. image:: https://img.shields.io/pypi/v/syncer.svg :target: https://pypi.python.org/pypi/syncer

.. image:: https://img.shields.io/pypi/pyversions/syncer.svg :target: https://pypi.python.org/pypi/syncer

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.

.. code-block:: py

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:

.. code-block:: py

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:

.. code-block:: py

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 <https://github.com/miyakogi/syncer/blob/master/test_syncer.py>_.

License

MIT license <https://github.com/miyakogi/syncer/blob/master/LICENSE>_

Credits

This package was created with Cookiecutter_ and the audreyr/cookiecutter-pypackage_ project template.

.. _Cookiecutter: https://github.com/audreyr/cookiecutter .. _audreyr/cookiecutter-pypackage: https://github.com/audreyr/cookiecutter-pypackage

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc