Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Mar 7, 2024
1 parent fec7fdd commit e820fbe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/tqdm_publisher/demo/client.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ <h1>tqdm_progress</h1>
constructor(props) {

this.#connect(props);

}

close() {
this.socket.close();
}

}


const bars = {}
const client = new ProgressClient({
Expand Down
18 changes: 9 additions & 9 deletions src/tqdm_publisher/demo/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,32 @@
import json
import random
import threading
import time
from typing import List
from uuid import uuid4
import time

import websockets

from tqdm_publisher import TQDMPublisher


def generate_task_durations(n = 100) -> List[float]:
def generate_task_durations(n=100) -> List[float]:
return [random.uniform(0, 1.0) for _ in range(n)]


def start_progress_bar(id, callback):
durations = generate_task_durations()
progress_bar = TQDMPublisher(durations)
progress_bar.subscribe(lambda info: callback(id, info))
for duration in progress_bar:
time.sleep(duration)


class WebSocketHandler:
def __init__(self):
self.clients = {}
pass


async def send(self, id, data):
await self.clients[id].send(json.dumps(data))

Expand All @@ -38,31 +39,30 @@ async def handler(self, websocket):

def on_progress(id, info):

asyncio.run(self.send(identifier, dict(
id=id,
payload=info
)))
asyncio.run(self.send(identifier, dict(id=id, payload=info)))

try:
async for message in websocket:

info = json.loads(message)

if (info["command"] == "start"):
if info["command"] == "start":
thread = threading.Thread(target=start_progress_bar, args=[info["id"], on_progress])
thread.start()

finally:
del self.clients[identifier] # This is called when the connection is closed
del self.clients[identifier] # This is called when the connection is closed


async def spawn_server():
handler = WebSocketHandler().handler
async with websockets.serve(handler, "", 8000):
await asyncio.Future()


def main():
asyncio.run(spawn_server())


if __name__ == "__main__":
main()

0 comments on commit e820fbe

Please sign in to comment.