{"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":"en","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,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}