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.
* PyPI: https://pypi.python.org/pypi/syncer/
* Documentation: https://miyakogi.github.io/syncer/
* Source code: https://github.com/miyakogi/syncer/
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
Raw data
{
"_id": null,
"home_page": "https://github.com/miyakogi/syncer",
"name": "syncer",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "syncer",
"author": "Hiroyuki Takagi",
"author_email": "miyako.dev@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/8d/dd/d4dd75843692690d81f0a4b929212a1614b25d4896aa7c72f4c3546c7e3d/syncer-2.0.3.tar.gz",
"platform": null,
"description": "Syncer\n======\n\n.. image:: https://img.shields.io/pypi/v/syncer.svg\n :target: https://pypi.python.org/pypi/syncer\n\n.. image:: https://img.shields.io/pypi/pyversions/syncer.svg\n :target: https://pypi.python.org/pypi/syncer\n\n\nSyncer is an async-to-sync converter for python.\n\n* PyPI: https://pypi.python.org/pypi/syncer/\n* Documentation: https://miyakogi.github.io/syncer/\n* Source code: https://github.com/miyakogi/syncer/\n\nFeatures\n========\n\nSometimes (mainly in test) we need to convert asynchronous functions to normal,\nsynchronous functions and run them synchronously. It can be done by\n``ayncio.get_event_loop().run_until_complete()``, but it's quite long...\n\nSyncer makes this conversion easy.\n\n* Convert async-function (defined by ``aync def``) to normal (synchronous) function\n* Evaluate coroutines synchronously\n\nInstall\n=======\n\nAt the command line::\n\n $ pip install syncer\n\nUsage\n=====\n\nThis module has only one function: ``syncer.sync``.\n\n.. code-block:: py\n\n from syncer import sync\n async def async_fun():\n ...\n return 1\n b = sync(async_fun) # now b is synchronous\n assert 1 == b()\n\nTo test the above ``async_fun`` in asynchronous test functions:\n\n.. code-block:: py\n\n import unittest\n\n class TestA(unittest.TestCase):\n # ``sync`` can be used as decorator.\n # The decorated function becomes synchronous.\n @sync\n async def test_async_fun(self):\n self.assertEqual(await async_fun(), 1)\n\nOr, keep test functions synchronous and get results synchronously:\n\n.. code-block:: py\n\n class TestA(unittest.TestCase):\n def test_async_fun(self):\n # run coroutine and return the result\n self.assertEqual(sync(async_fun()), 1)\n # This is equivalent to below, just a shortcut\n self.assertEqual(\n asyncio.get_event_loop().run_until_complete(async_fun()), 1)\n\nMore examples/use-cases will be found in `test <https://github.com/miyakogi/syncer/blob/master/test_syncer.py>`_.\n\nLicense\n=======\n\n`MIT license <https://github.com/miyakogi/syncer/blob/master/LICENSE>`_\n\nCredits\n=======\n\nThis package was created with Cookiecutter_ and the `audreyr/cookiecutter-pypackage`_ project template.\n\n.. _Cookiecutter: https://github.com/audreyr/cookiecutter\n.. _`audreyr/cookiecutter-pypackage`: https://github.com/audreyr/cookiecutter-pypackage\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Async to sync converter",
"version": "2.0.3",
"project_urls": {
"Homepage": "https://github.com/miyakogi/syncer"
},
"split_keywords": [
"syncer"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "8dddd4dd75843692690d81f0a4b929212a1614b25d4896aa7c72f4c3546c7e3d",
"md5": "4152a113474fc09e2c0bde4894f8b0e7",
"sha256": "4340eb54b54368724a78c5c0763824470201804fe9180129daf3635cb500550f"
},
"downloads": -1,
"filename": "syncer-2.0.3.tar.gz",
"has_sig": false,
"md5_digest": "4152a113474fc09e2c0bde4894f8b0e7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 11512,
"upload_time": "2023-05-08T07:50:17",
"upload_time_iso_8601": "2023-05-08T07:50:17.963497Z",
"url": "https://files.pythonhosted.org/packages/8d/dd/d4dd75843692690d81f0a4b929212a1614b25d4896aa7c72f4c3546c7e3d/syncer-2.0.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-05-08 07:50:17",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "miyakogi",
"github_project": "syncer",
"travis_ci": false,
"coveralls": true,
"github_actions": false,
"tox": true,
"lcname": "syncer"
}