Skip to main content

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.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bsclock 0.1.1
File Size Uploaded Provenance Actions
bsclock-0.1.1.tar.gz 17.4 kB With provenance Details for bsclock-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bsclock 0.1.1
File Interpreter ABI Platform Actions
bsclock-0.1.1-py3-none-any.whl Python 3 none any Details for bsclock-0.1.1-py3-none-any.whl

Total release size: 27.3 kB

Release files / bsclock-0.1.1.tar.gz

Download URL bsclock-0.1.1.tar.gz
Size 17.4 kB
Tags Source
SHA-256 checksum
How to use checksums
baeeadd4c21a7a2bfc4354ed84cb06a15112e9c96cb86f0e83d9a7b98d813427
BLAKE2b-256 checksum
How to use checksums
0ec587d25466ec04330d928b8a849cd520db4c6574b3eb9590cab4c6a186d0e0
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 log

Release files / bsclock-0.1.1-py3-none-any.whl

Download URL bsclock-0.1.1-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
35a0c36f192cfa228cb0f8c3b02d1fdd6438b7cdecfdf64443eaaaa265b5a699
BLAKE2b-256 checksum
How to use checksums
dfaaf18cef465882b69d9b8b8186ba270c2561f1b1e718c48b295dbe7312b9c2
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 log

Security for bsclock 0.1.1

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.

Select from 2 files

Attestation for bsclock-0.1.1.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 log

A 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.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page