0

I'm trying to mock a websockets data stream and I'm getting this error: 'async_generator' object is not an iterator

This is my generator code:

from time import sleep


mock_sf_record = '{"payload": ...}'


async def generateMessages():
    sleep(5)
    yield mock_sf_record

and the code that calls this code:

async def subscribe(subscription):
    global RECEIVED_MESSAGES_CACHE
    ...
    while True:
        messageStream = await(next(generateMessages())) if ENV == 'dev' else await websocket.recv()

What can I do? What am I doing wrong? I'm basically using the generateMessages() generator to create a stream of messages, but this isn't working...

The code that is calling subscribe:

 for subscription in SUBSCRIPTION_TYPES:
        loop.create_task(subscribe(subscription))
    loop.run_forever()

More importantly, if I change the code to use a synchronous generator, this only generates messages for a single subscription and I never seem to generate messsages for any other subscription... it seems to block on a single thread. Why is this?

messageStream = (next(generateMessages())) if ENV == 'dev' else await websocket.recv()

and

# generator that generates mock SF data
from asyncio import sleep

mock_sf_record = '{"payload": ...}'


def generateMessages():
    sleep(5)
    yield mock_sf_record

Why does the synchronous generator cause problems?

1

1 Answer 1

0

The right way:

async def subscribe(subscription):
    global RECEIVED_MESSAGES_CACHE
    ...
    gen = generateMessages()    # init async generator
    messageStream = (await gen.__anext__()) if ENV == 'dev' else (await websocket.recv())

https://www.python.org/dev/peps/pep-0525/#support-for-asynchronous-iteration-protocol

Sign up to request clarification or add additional context in comments.

3 Comments

do I have to change anything else? Do I have to implemenet the __anext()) method?
@Jwan622, you may in case if you want to use a custom class implementation instead (python.org/dev/peps/pep-0525/#rationale-and-goals) But your case is too simple to bother
wrote another quick question for you @RomanPerekhrest relatedly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.