BSClock
BSClock runs Python asyncio applications on simulation time. Use it to test a timer-driven service, a protocol host, or a simulator control loop with an application-supplied clock. With a BabbleSim device clock, the Python loop can share the same simulation timeline as native radio participants.
Install
Use Linux and Python 3.11 or newer. BSClock uses the Python standard library.
python3 -m venv .venv
. .venv/bin/activate
python -m pip install bsclock
Samples
Download the linked scripts or use the copies under samples/ in the source distribution. Run the commands from the directory containing the scripts.
Drive timers with your own clock
timers.py supplies an in-memory clock. The standard asyncio timer fires at 2 ms, and a subsequent 3 ms sleep ends at 5 ms, both in simulation time.
python timers.py
Callback at 2000 us
Sleep completed at 5000 us
Share time with a BabbleSim PHY
babblesim_clock.py launches a PHY with one slot for the clock, waits for the first grant, then runs an asyncio timer.
Install bs2g4phy for this example and follow its native PHY setup to build the PHY, BLE modem, and default NtNcable channel.
python -m pip install bs2g4phy
export BSIM_OUT_PATH=/path/to/babblesim/build
python babblesim_clock.py
First PHY grant at 0 us
Sleep completed at 5000 us
PHY log: babblesim-clock-logs/phy.log
Python API
Pass SimulationEventLoop as the loop factory to asyncio.Runner. Bind any clock that exposes now_us and advance_to_us(target_us). The clock uses integer microseconds. While it is bound, pass delays and deadlines to asyncio in seconds, aligned to whole microseconds. This applies to asyncio.sleep(), loop.call_later(), and loop.call_at(). The loop allows floating-point roundoff and raises ValueError for deadlines between simulation ticks.
Bind the clock before scheduling application timers. After loop.unbind_clock(), time advances at wall-clock speed from the last simulation time. Pending timers keep their deadlines.
For an existing BabbleSim simulation, reserve a device ID for bs2g4phy.BabbleSimClock. Connect the other participants and the clock, obtain the first grant with clock.advance_to_us(0), then call loop.bind_clock(clock). During the run, the loop advances only after the PHY acknowledges the requested time. Unbind the loop and close the clock as part of the simulation teardown.
Schedule work before advancing time
Pass an on_idle(deadline_us) callback to SimulationEventLoop to admit pending work when asyncio has no ready callbacks or I/O. The loop calls it before an idle wait or clock advance. The argument is the next timer's absolute deadline in simulation microseconds, or None if no simulation clock or timer is active.
Return True after scheduling ready work to give it a turn at the current time. Return False to let the loop continue its usual wait or advance. The callback runs synchronously and should only decide whether to schedule work.
The clock and max_step_us properties expose the bound clock and maximum idle step. The default step is bsclock.DEFAULT_MAX_STEP_US (1,000 µs).
Develop from source
From components/bsclock in the repository checkout, install the test tools and run the tests.
python -m pip install -e '.[test]'
python -m pytest
python -m build
The bsclock-v<version> release tag selects this package in the PyPI workflow. Its version must match pyproject.toml, and the workflow uses the pypi-bsclock Trusted Publisher environment.
Metadata
Release files for bsclock 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| bsclock-0.1.0.tar.gz | 17.4 kB | With provenance | Details for bsclock-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| bsclock-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for bsclock-0.1.0-py3-none-any.whl |
Total release size: 27.4 kB
Release files / bsclock-0.1.0.tar.gz
| Download URL | bsclock-0.1.0.tar.gz |
|---|---|
| Size | 17.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6c851de8130d42c1a1ef80c51ea63db7cc0683cbb6b7891ff40d51b72223c308
|
|
BLAKE2b-256 checksum How to use checksums |
70e2de0ce9ab59d95a97a82baf0efb2efa4512f6aacf61617b5670f5b07db56c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.
Transparency logRelease files / bsclock-0.1.0-py3-none-any.whl
| Download URL | bsclock-0.1.0-py3-none-any.whl |
|---|---|
| Size | 10.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
06d5baa0e5521f524a42aebf8ebe77514ae6ebab0ebb6083a60f64f5216a3daa
|
|
BLAKE2b-256 checksum How to use checksums |
4003d126d0c3768f724d7553026e0709c2bf74a19c998d26387e6c344197f316
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.
Transparency logSecurity for bsclock 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for bsclock-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.