Report this

What is the reason for this report?

How do I use my droplet as a server using the python websockets module?

Posted on April 15, 2020

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!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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:

  1. 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.

Explanation:

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:

Solution:

  1. Check the Environment:

    • If you’re running the code in an environment that already manages an event loop (e.g., Jupyter Notebook or certain IDEs), try running your code directly from the command line or terminal. This avoids the problem entirely.
  2. Use await instead of run_until_complete():

    • Since you already have an event loop running, you can directly 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+):

  • The 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())

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.