#๐Ÿ”’ Help with Asyncio & Websockets

6 messages ยท Page 1 of 1 (latest)

cloud hill
#

I currently have a very basic websocket server impl:

import asyncio

import websockets
from websockets.server import serve

async def run():
    async with serve(handler, "localhost", 8765):
        print("Websocket server up")
        await asyncio.Future()

I have another file where I import this function. I would like to run this non-blocking since I also have a discord bot running.

import asyncio

import config
import discord_bot
import websocket_server

if __name__ == "__main__":
    asyncio.create_task(websocket_server.run())
    discord_bot.bot.run(config.DISCORD_TOKEN)```

When running this though, I receive the error `RuntimeError: no running event loop`. How do I fix this?
main tulipBOT
#

@cloud hill

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

cloud hill
#

Figured it out; you can just do:

async def main():
    websocket_server_task = asyncio.create_task(websocket_server.run())
    discord_bot_task = asyncio.create_task(bot.start(config.DISCORD_TOKEN))

    await asyncio.gather(websocket_server_task, discord_bot_task)


if __name__ == "__main__":
    asyncio.run(main())```
#

any better sols?

main tulipBOT
#

@cloud hill

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.