By robertwdg
I’m a complete beginner to network programming. I’m writing a program to allow me to play board games with my friends throughout isolation (I know, I know, there are already similar programs available online, but I want to make my own to make myself a better programmer). I’m trying to use the websockets library on python to allow me and my friends to connect to the droplet I’ve set up. When my client tried to connect to the server I get the following error:
RuntimeError Traceback (most recent call last)
~\Desktop\test_program\client2.py in <module>
23
24
---> 25 asyncio.get_event_loop().run_until_complete(communicate())
~\AppData\Local\Continuum\anaconda3\lib\asyncio\base_events.py in run_until_complete(self, future)
558 future.add_done_callback(_run_until_complete_cb)
559 try:
--> 560 self.run_forever()
561 except:
562 if new_task and future.done() and not future.cancelled():
~\AppData\Local\Continuum\anaconda3\lib\asyncio\base_events.py in run_forever(self)
513 self._check_closed()
514 if self.is_running():
--> 515 raise RuntimeError('This event loop is already running')
516 if events._get_running_loop() is not None:
517 raise RuntimeError(
RuntimeError: This event loop is already running
Does anyone know what I might be doing incorrectly? Could it be an issue with the firewall on my computer or on my droplet?
Thanks, Robert
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi there @robertwdg,
It does not look like a firewall problem. I can see that there was a similar problem reported here and what fixed the problem for the user was installing nest_asyncio:
- pip install nest_asyncio
And adding the following lines:
import nest_asyncio
nest_asyncio.apply()
Let me know how it goes! Regards, Bobby
Heya,
Another approach would be to error you’re encountering, RuntimeError: This event loop is already running, is not related to network firewalls or your droplet’s configuration but rather to how you’re using asyncio in your program.
This error occurs because you’re trying to run a new event loop (run_until_complete) in an environment where an event loop is already running. This is common in environments like Jupyter Notebooks, or in certain integrated development environments (IDEs) like Spyder or PyCharm.
Here’s how to address it:
Check the Environment:
Use await instead of run_until_complete():
await your coroutine instead of using asyncio.get_event_loop().run_until_complete(). This method works inside an already running event loop.Here’s an example of how you can modify your code:
import asyncio
import websockets
async def communicate():
async with websockets.connect('ws://your_droplet_ip:port') as websocket:
# Your communication logic here
pass
# Instead of run_until_complete, use await if you're already in an event loop
if __name__ == "__main__":
asyncio.run(communicate()) # Use asyncio.run in Python 3.7+
Use asyncio.run() (Python 3.7+):
asyncio.run() function was introduced in Python 3.7 to run a coroutine and manage the event loop cleanly. This function simplifies the process and avoids the issue of a running event loop.Replace asyncio.get_event_loop().run_until_complete(communicate()) with:
asyncio.run(communicate())
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.