1

I want to execute powers sync function in a different thread and group the returned value from powers whenever I need it in my main script. I have tried the Threading module of python, but it didn't work for me. I noticed that the async generator async for power in powers(): doesn't stop even if I put a condition to break to loop, that's why I was thinking maybe I can run this loop in different thread and get the returned value when I need it.

Here is my code :

import asyncio
from rtlsdr import RtlSdr
import numpy as np



async def powers():
    # SDR CONFIGURATION
    sdr = RtlSdr()
    sdr.rs = 2.4e6
    sdr.fc = 801e6

    async for samples in sdr.stream(512):
        samples = samples - np.mean(samples)
        calibrate = 3.2
        power = np.mean(np.abs(samples ** 2))
        yield power
        # print('Relative power:', calibrate * 10 * np.log10(power), 'dB')
    await sdr.stop()
    sdr.close()

async def main():
    nb_config = 1000
    fpower = -100
    i = 0

    while i < nb_config:
        async for power in powers():
            print(i)
            print(10 * np.log10(power))
            if power > fpower:
                fpower = power
            i+= 1        #print(10 * np.log10(fpower))
            if i >= nb_config:
                break
asyncio.run(main()) 
quamrana
  • 37,849
  • 12
  • 53
  • 71

0 Answers0