Registry / testing / syncer

syncer

JSON →
library2.0.3pypypi✓ verified 90d ago

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.

pip install syncer
INSTALL
IMPORT
SIG · SYNCER
S
syncer
testingpythonv2.0.3
Install
2.6s avg
Import
201ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v2.0.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.216s · 19.2MB
glibc
py 3.10–3.920 runs
installs and imports cleanly · install 2.6s · import 0.186s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

sync
✓ from syncer import sync

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.

import asyncio from syncer import sync async def fetch_data(): await asyncio.sleep(0.01) # Simulate an async I/O operation return "Async data fetched!" # --- Usage 1: Run an async function synchronously (returns a callable) --- synchronous_fetch = sync(fetch_data) result_function_call = synchronous_fetch() # Call the returned synchronous function print(f"Result from function call: {result_function_call}") # --- Usage 2: Run a coroutine synchronously (returns the coroutine's result) --- result_coroutine_run = sync(fetch_data()) print(f"Result from coroutine run: {result_coroutine_run}") # --- Usage 3: As a decorator for synchronous test methods --- import unittest class MySyncTests(unittest.TestCase): @sync async def test_async_fetch(self): data = await fetch_data() self.assertEqual(data, "Async data fetched!") # To run the tests (optional, for demonstration): # if __name__ == '__main__': # unittest.main()
Debug
Known issues
gotchaWhile `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.
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.
affects: All versions
gotcha`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.
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`.
affects: All versions
gotchaBe 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.
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.
affects: All versions
Upgrade
Version history
2.0.3latest on PyPI · released May 8, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
18 hits · last 30 days
node
14
Resources
syncer — pip install syncer · libregistry