Simulating Multiple Servers #1272
-
Hi Guys, I'm trying to simulate multiple servers using asyncio.
Is simulating multiple servers using Asyncio possible? if so, did I make any mistakes? |
Beta Was this translation helpful? Give feedback.
Answered by
AndreasHeine
Apr 4, 2023
Replies: 2 comments 2 replies
-
Yes. This is no problem. Yes your code is completely wrong. Take some python asyncio tutorial first |
Beta Was this translation helpful? Give feedback.
1 reply
-
each Server needs to be in a own asyncio task! e.g.: async def server1():
# create the server here
pass
async def server2():
# create the server here
pass
async def main():
task1 = asyncio.create_task(server1())
task2 = asyncio.create_task(server2())
await asyncio.gather(task1, task2)
if __name__ == "__main__":
# since python 3.8 has changed the default event loop
# only if your on windows
if sys.platform.lower() == "win32" or os.name.lower() == "nt":
from asyncio import (
set_event_loop_policy,
WindowsSelectorEventLoopPolicy
)
set_event_loop_policy(WindowsSelectorEventLoopPolicy())
asyncio.run(main()) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
JiaNannnn
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
each Server needs to be in a own asyncio task!
e.g.: