Skip to content

Example 3.4: Waiting for multiple events. Game loop stops after disconnect #10

@vug

Description

@vug

Thanks for amazing tutorial! I am interested in asynchronous network programming for multiplayer games and yours is the best resource I encountered.

I run game_loop_wait.py with Python 3.5. I open the client page on a browser. Click on Connect. Get connecting, connected and game loop ticks messages. When I press keys I get Pressed Key Code: X messages from the server. Then after waiting more than a second get game loop ticks again. I can open the client on another tab and experience same stuff. Everything is fine up to this point.

Then if any of the clients click on Disconnect 1) game loop stops, 2) wshandler for new clients who connect after one client has disconnected hangs at await tick.acquire()

I added this print statement to game loop to see its stopping:

async def game_loop():
    cnt = 0
    while 1:
        print('game loop tick no {}'.format(cnt))
        cnt += 1
        await tick.acquire()
        tick.notify_all()
        tick.release()
        await asyncio.sleep(1)

And added this print statement to see where the handlers get stuck:

        if not tick_task:
            print('no tick task. await tick.acquire()')
            await tick.acquire()
            print('await tick.acquire() done')
            tick_task = asyncio.ensure_future(tick.wait())

I'd like to solve the issue and send a PR however I have no former experience with asyncio. :-( Your previous examples allow disconnection and reconnection by the clients. Hence, I hope I didn't get the intention of example 3.4 incorrectly.

Have a good day!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions