Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions w2/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
from w2.utils.websocket import ConnectionManager
from w2.utils.response_model import ProcessStatus
from w2.utils.database import DB
from contextlib import asynccontextmanager

app = FastAPI()
manager = ConnectionManager()

# start an asynchronous task that will keep broadcasting the process status to all the connected clients
broadcast_continuous = Thread(target=asyncio.run, args=(manager.broadcast_all(),))
broadcast_continuous.start()
@asynccontextmanager
async def lifespan(app: FastAPI):
asyncio.create_task(manager.broadcast_all())
yield

app = FastAPI(lifespan=lifespan)

# The below endpoint is used to create websocket connection
@app.websocket("/ws")
Expand Down
3 changes: 2 additions & 1 deletion w2/utils/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ async def broadcast_all(self):
processes = self.db.read_all()
await connection.send_json(processes)

time.sleep(1)
import asyncio
await asyncio.sleep(1)

except Exception as e:
print(e)