question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is safe to remove value from list in asyncio

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

class ConnectionManager:
    def __init__(self):
        self.active_connections: List[WebSocket] = []

    async def connect(self, websocket: WebSocket):
        await websocket.accept()
        self.active_connections.append(websocket)

    def disconnect(self, websocket: WebSocket):
        self.active_connections.remove(websocket)

    async def send_personal_message(self, message: str, websocket: WebSocket):
        await websocket.send_text(message)

    async def broadcast(self, message: str):
        for connection in self.active_connections:
            await connection.send_text(message)

Description

I find the example from doc.

I am wondering that it is safe to remove value from list.In this example, when websocketA broadcast message, await websocketB.send_text(message), websocketB is disconnect, what would happen?

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.78.0

Python Version

python3.8.13

Additional Context

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
JarroVGITcommented, Jun 26, 2022

I would disagree with that statement. The purpose of the code block is to give an example on how to handle multiple connections and disconnects. It’s purpose is not to demonstrate how that should work in a threadsafe manner.

The code as-is, is perfectly safe to use. If you need multithreading or multiprocessing, then it is up to the developer to think about the ramifications and thing like race conditions. I don’t feel that should be very explicit in the examples though, as it just might confuse people more rather than helping the point of explaining how websockets work in FastAPI

0reactions
xiazhibincommented, Jun 28, 2022

Yes, as a example it’s pretty good! Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Remove all the occurrences of an element from a list in Python
Method 3 : Using remove() In this method, we iterate through each item in the list, and when we find a match for...
Read more >
Remove element from a Python LIST [clear, pop, remove, del]
To remove an element from the list, you can use the del keyword followed by a list. You have to pass the index...
Read more >
[Example code]-how to remove value from list safely in asyncio
There is a global list to store data. Different async functions maybe add or remove value from it. Example: a = [] #...
Read more >
Python List .remove() - How to Remove an Item from a List in ...
To remove an element from a list using the remove() method, specify the value of that element and pass it as an argument...
Read more >
How to delete an element from a list in Python - Educative.io
remove (). remove() deletes the first instance of a value in a list. · del. del can be used to delete a single...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found