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 25, 2024
1 parent d548f5c commit 4055351
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/tqdm_publisher/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ._handler import TQDMProgressHandler
from ._publisher import TQDMPublisher
from ._subscriber import TQDMProgressSubscriber
from ._handler import TQDMProgressHandler

__all__ = ["TQDMPublisher", "TQDMProgressSubscriber", "TQDMProgressHandler"]
2 changes: 1 addition & 1 deletion src/tqdm_publisher/_demos/_client.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ header {
height: 100%;
background-color: #4caf50;
width: 0%;
}
}
3 changes: 1 addition & 2 deletions src/tqdm_publisher/_demos/_demo_command_line_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
from pathlib import Path

from tqdm_publisher._demos._multiple_bars._server import run_multiple_bar_demo
from tqdm_publisher._demos._single_bar._server import run_single_bar_demo
from tqdm_publisher._demos._parallel_bars._server import run_parallel_bar_demo

from tqdm_publisher._demos._single_bar._server import run_single_bar_demo

CLIENT_PORT = 1234

Expand Down
4 changes: 2 additions & 2 deletions src/tqdm_publisher/_demos/_parallel_bars/_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ function getRequestContainer(request_id) {

// Create and/or render a progress bar
const getBar = (request_id, id) => {

if (bars[id]) return bars[id];

const container = getRequestContainer(request_id);
const bar = createProgressBar(container);

return bars[id] = bar;

}

// Update the specified progress bar when a message is received from the server
Expand Down
2 changes: 1 addition & 1 deletion src/tqdm_publisher/_demos/_parallel_bars/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def signal_handler(signal, frame):
try:
signal.signal(signal.SIGINT, signal_handler)
except:
pass # Allow to work in thread
pass # Allow to work in thread

print(f"Serving HTTP on port {port}")
httpd.serve_forever()
Expand Down
36 changes: 17 additions & 19 deletions src/tqdm_publisher/_demos/_parallel_bars/_server.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
"""Demo of parallel tqdm."""

import asyncio
import json
import sys
import threading
import time
import uuid
from concurrent.futures import ProcessPoolExecutor
from typing import List

import requests

from tqdm_publisher import TQDMPublisher, TQDMProgressHandler

import asyncio
import json
import time
import websockets
import threading
from tqdm_publisher._demos._parallel_bars._client import create_http_server, find_free_port

from tqdm_publisher import TQDMProgressHandler, TQDMPublisher
from tqdm_publisher._demos._parallel_bars._client import (
create_http_server,
find_free_port,
)

N_JOBS = 3

Expand All @@ -36,6 +36,7 @@
## NOTE: TQDMProgressHandler cannot be called from a process...so we just use a queue directly
progress_handler = TQDMProgressHandler()


def forward_updates_over_websocket(request_id, id, n, total, **kwargs):
ws = WEBSOCKETS.get(request_id)

Expand Down Expand Up @@ -92,9 +93,9 @@ def forward_to_http_server(url: str, request_id: str, id: int, n: int, total: in


def _run_sleep_tasks_in_subprocess(
args,
# task_times: List[float], iteration_index: int, id: int, url: str
):
args,
# task_times: List[float], iteration_index: int, id: int, url: str
):
"""
Run a 'task' that takes a certain amount of time to run on each worker.
Expand Down Expand Up @@ -146,10 +147,7 @@ def run_parallel_processes(request_id, url: str):
# Assign the parallel jobs
job_map = executor.map(
_run_sleep_tasks_in_subprocess,
[
(task_times, iteration_index, request_id, url)
for iteration_index, task_times in enumerate(TASK_TIMES)
],
[(task_times, iteration_index, request_id, url) for iteration_index, task_times in enumerate(TASK_TIMES)],
)

# Perform iteration to deploy jobs
Expand All @@ -159,6 +157,7 @@ def run_parallel_processes(request_id, url: str):

WEBSOCKETS = {}


async def handler(url: str, websocket: websockets.WebSocketServerProtocol) -> None:
"""Handle messages from the client and manage the client connections."""

Expand All @@ -173,6 +172,7 @@ async def handler(url: str, websocket: websockets.WebSocketServerProtocol) -> No
WEBSOCKETS[request_id] = dict(ref=websocket, id=connection_id)
run_parallel_processes(request_id, url)


async def spawn_server() -> None:
"""Spawn the server asynchronously."""

Expand All @@ -182,7 +182,6 @@ async def spawn_server() -> None:

async with websockets.serve(ws_handler=lambda websocket: handler(URL, websocket), host="", port=8000):


# DEMO ONE: Direct updates from HTTP server
http_server = ThreadedHTTPServer(port=PORT, callback=forward_updates_over_websocket)
http_server.start()
Expand All @@ -191,10 +190,10 @@ async def spawn_server() -> None:
# # DEMO TWO: Queue
# def update_queue(request_id, id, n, total, **kwargs):
# progress_handler._announce(dict(request_id=request_id, id=id, n=n, total=total))

# http_server = ThreadedHTTPServer(port=PORT, callback=update_queue)
# http_server.start()

# queue_task = ThreadedQueueTask()
# queue_task.start()
# await asyncio.Future()
Expand Down Expand Up @@ -230,4 +229,3 @@ def run_parallel_bar_demo() -> None:
# Just run the parallel processes
request_id = uuid.uuid4()
run_parallel_processes(request_id, URL)

1 change: 1 addition & 0 deletions src/tqdm_publisher/_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from ._subscriber import TQDMProgressSubscriber


class TQDMProgressHandler:
def __init__(self):
self.listeners = []
Expand Down
1 change: 1 addition & 0 deletions src/tqdm_publisher/_subscriber.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ._publisher import TQDMPublisher


class TQDMProgressSubscriber(TQDMPublisher):
def __init__(self, iterable, on_progress_update: callable, **tqdm_kwargs):
super().__init__(iterable, **tqdm_kwargs)
Expand Down

0 comments on commit 4055351

Please sign in to comment.