This module adds asyncio support for reading samples from the device.

The main functionality can be found in the stream() method of rtlsdr.rtlsdraio.RtlSdrAio.


import asyncio
from rtlsdr import RtlSdr

async def streaming():
    sdr = RtlSdr()

    async for samples in sdr.stream():
        # do something with samples
        # ...

    # to stop streaming:
    await sdr.stop()

    # done

loop = asyncio.get_event_loop()
class rtlsdr.rtlsdraio.RtlSdrAio(device_index=0, test_mode_enabled=False, serial_number=None)[source]

Bases: rtlsdr.rtlsdr.RtlSdr


Stop async stream

Stops the read_samples_async and Excecutor task created by stream().

stream(num_samples_or_bytes=131072, format='samples', loop=None)[source]

Start async streaming from SDR and return an async iterator (Python 3.5+).

The read_samples_async() method is called in an Excecutor instance using asyncio.AbstractEventLoop.run_in_executor().

The returned asynchronous iterable can then used to retrieve sample data using async for syntax.

Calling the stop() method will stop the read_samples_async session and close the Excecutor task.

  • num_samples_or_bytes (int) – The number of bytes/samples that will be returned each iteration

  • format (str, optional) – Specifies whether raw data (“bytes”) or IQ samples (“samples”) will be returned

  • loop (optional) – An asyncio event loop


An asynchronous iterator to yield sample data

class rtlsdr.rtlsdraio.AsyncCallbackIter(func_start, func_stop=None, queue_size=20, *, loop=None)

Bases: object

Convert a callback-based legacy async function into one supporting asyncio and Python 3.5+

The queued data can be iterated using async for

  • func_start – A callable which should take a single callback that will be passed data. Will be run in a seperate thread in case it blocks.

  • func_stop (optional) – A callable to stop func_start from calling the callback. Will be run in a seperate thread in case it blocks.

  • queue_size (int, optional) – The maximum amount of data that will be buffered.

  • loop (optional) – The asyncio.event_loop to use. If not supplied, asyncio.get_event_loop() will be used.

async add_to_queue(*args)

Add items to the queue


*args – Arguments to be added

This method is a coroutine

async start()

Start the execution

The callback given by func_start will be called by asyncio.AbstractEventLoop.run_in_executor() and will continue until stop() is called.

This method is a coroutine

async stop()

Stop the running executor task

If func_stop was supplied, it will be called after the queue has been exhausted.

This method is a coroutine