How would I re-write this @async.coroutine using async?

I am struggling to port my @async.coroutine <=3.7 to new async 3.9. When I do, I can’t return anything and it forces every caller in the stack to also be labeled async which I cannot do.

            def get_result(q, func, sleep, pid, timeout):
                import queue
                import time

                while True:
                        yield time.sleep(sleep)

                        _result = q.get_nowait()
                        return _result
                    except queue.Empty:
                        import time
                        yield  time.sleep(sleep)

I get the results using this.

     _tasks += [get_result(queue, arg, self.sleep, pid, self.timeout)]
     tasks = asyncio.gather(*_tasks)

To be clear, I need.

  1. Coroutine has to return a value
  2. Coroutine has to be able to yield to event loop
  3. Calling function cannot also be async

This is the way it currently works. But I can’t make it work this way in 3.9

Any help is appreciated.

If timeout is not needed, why not write async def get_result(q): return await q.get()? If timeout is needed, I think most of your code will work if you replace time.sleep with asyncio.sleep and yield with await.What problems are you running into when you do that?

How are you invoking the function now?