{"id":6261,"library":"syncer","title":"Syncer","description":"Syncer is a Python library designed to simplify the conversion of asynchronous functions to synchronous ones. It abstracts away the boilerplate of `asyncio.get_event_loop().run_until_complete()`, making it easier to call `async def` functions from synchronous contexts, particularly useful in testing or when integrating with synchronous codebases. The current version is 2.0.3, with releases occurring periodically to maintain compatibility and add features.","status":"active","version":"2.0.3","language":"python","source_language":"en","source_url":"https://github.com/miyakogi/syncer","tags":["async","sync","utility","concurrency","testing","asyncio"],"install":[{"cmd":"pip install syncer","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"sync","correct":"from syncer import sync"}],"quickstart":{"code":"import asyncio\nfrom syncer import sync\n\nasync def fetch_data():\n    await asyncio.sleep(0.01) # Simulate an async I/O operation\n    return \"Async data fetched!\"\n\n# --- Usage 1: Run an async function synchronously (returns a callable) ---\nsynchronous_fetch = sync(fetch_data)\nresult_function_call = synchronous_fetch() # Call the returned synchronous function\nprint(f\"Result from function call: {result_function_call}\")\n\n# --- Usage 2: Run a coroutine synchronously (returns the coroutine's result) ---\nresult_coroutine_run = sync(fetch_data())\nprint(f\"Result from coroutine run: {result_coroutine_run}\")\n\n# --- Usage 3: As a decorator for synchronous test methods ---\nimport unittest\n\nclass MySyncTests(unittest.TestCase):\n    @sync\n    async def test_async_fetch(self):\n        data = await fetch_data()\n        self.assertEqual(data, \"Async data fetched!\")\n\n# To run the tests (optional, for demonstration):\n# if __name__ == '__main__':\n#     unittest.main()","lang":"python","description":"This quickstart demonstrates the primary ways to use `syncer.sync`. It can convert an `async def` function into a synchronous callable, directly execute a coroutine object to retrieve its result, or be used as a decorator on `async def` test methods to run them in a synchronous test runner."},"warnings":[{"fix":"Ensure `syncer.sync` is called from a truly synchronous execution point, typically the top-level of a script or a synchronous test method, not nested within another active asynchronous function call stack.","message":"While `syncer.sync` allows running async code from a synchronous context, it does so by managing an `asyncio` event loop. If `sync()` is called from within an already running `async def` function or an existing event loop, it will raise a `RuntimeError` (e.g., 'Cannot run loop while another loop is running'). It's intended for bridging from non-async contexts.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For blocking I/O within async functions, consider using `asyncio.to_thread()` to run the blocking operation in a separate thread, preventing it from stalling the event loop, even when the async function is then run via `syncer.sync`.","message":"`syncer` converts an async function or coroutine to run synchronously, but it does not magically make blocking I/O operations (e.g., `requests.get()`, `time.sleep()`) non-blocking if they occur within the async function's execution. If your async function internally calls long-running synchronous code, `syncer.sync` will still block the thread it's running on, potentially impacting performance or responsiveness.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always ensure you are passing either the async function (to get a synchronous wrapper) or the coroutine (to get its result) as intended. The quickstart examples illustrate both patterns clearly.","message":"Be mindful of the distinction between passing an `async def` function itself versus a coroutine object. `sync(async_func)` returns a *synchronous callable* that you then execute (e.g., `sync(async_func)()`). In contrast, `sync(async_func())` immediately executes the *coroutine object* returned by calling `async_func()` and returns its final result. Incorrect usage can lead to unexpected behavior or `TypeError` if you forget to call the returned synchronous function.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"search_vec":"'2.0.3':55 'abstract':19 'add':64 'async':35,66 'asynchron':13 'asyncio':71 'asyncio.get':24 'away':20 'boilerpl':22 'call':34 'codebas':50 'compat':62 'complet':29 'concurr':69 'context':40 'convers':11 'current':52 'def':36 'design':7 'easier':32 'event':25 'featur':65 'function':14,37 'integr':47 'librari':6 'loop':26 'maintain':61 'make':30 'occur':58 'one':17 'particular':41 'period':59 'python':5 'releas':57 'run':27 'simplifi':9 'sync':67 'syncer':1,2 'synchron':16,39,49 'test':44,70 'use':42 'util':68 'version':53","created_at":"2026-04-14T18:48:57.046589+00:00","updated_at":"2026-04-14T18:48:57.046589+00:00","problems":[],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"2.0.3","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/miyakogi/syncer","docs":null,"changelog":null,"pypi":"https://pypi.org/project/syncer/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["testing"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-06-28","next_check":"2026-07-28","install_tag":null}}