Skip to content

Commit f87072c

Browse files
committed
Add flush print()
1 parent 739e543 commit f87072c

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ OUTLINE
3939
- [get_logs()](#get_logs)
4040
- [get_result_info()](#get_result_info)
4141
- [get_tps()](#get_tps)
42+
- [print()](#printobjects-sep--endn-filenone-flushtrue)
4243
- [Scenarios](#scenarios)
4344
- [Stress Test](#stress-test)
45+
- [Background Execution](#background-execution)
4446
- [Appendix](#appendix)
4547
- [Mode Explanation](#mode-explanation)
4648

@@ -307,6 +309,9 @@ callback_on_all_done_function (id: int, config, result, log: dict) -> Any
307309
* Peak detected above the current TPS threshold - TPS: 55.53846153846154, Started at: 1734937212, Ended at: 1734937225
308310
```
309311

312+
- #### print(*objects, sep=' ', end='\n', file=None, flush=True)
313+
A custom print function that sets `flush=True` by default to ensure immediate output to stdout, useful when redirecting output to a file.
314+
310315
### Scenarios
311316

312317
#### Stress Test
@@ -348,6 +353,37 @@ print(worker_dispatcher.get_tps())
348353

349354
> The stress tool, based on this dispatcher, along with statistical TPS reports, is as follows: [yidas / python-stress-tool](https://github.com/yidas/python-stress-tool)
350355

356+
357+
#### Background Execution
358+
359+
You can run the script as a background process by adding & at the end of the command, and redirect the output to a file:
360+
361+
```
362+
$ python3 main.py > log.txt 2>&1 &
363+
```
364+
365+
> `2>&1` means redirecting stderr (2) to the same location as stdout (1), so both standard output and error messages go to the same file.
366+
367+
For immediate stdout output (e.g., when logging to a file), use `worker_dispatcher.print()`, which enables flushing by default. Alternatively, use the built-in `print(..., flush=True)`.
368+
369+
```python
370+
import worker_dispatcher
371+
372+
def each_task(id, config, task, metadata):
373+
# Print immediately to file (stdout is flushed)
374+
if id % 10 == 0:
375+
worker_dispatcher.print(f"TaskId: {id}")
376+
return True
377+
378+
responses = worker_dispatcher.start({
379+
'task': {
380+
'list': 600,
381+
'function': each_task,
382+
...
383+
})
384+
```
385+
386+
351387
---
352388

353389
## Appendix

src/worker_dispatcher/worker_dispatcher.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import multiprocessing, concurrent.futures
2-
import time, datetime, copy, requests, math, os, platform
2+
import time, datetime, copy, requests, math, os, platform, builtins
33
import queue as Queue
44

55
# Sample task function
@@ -872,6 +872,11 @@ def _search_peak_by_start_time_write(wrap_references, current_tps, start_time, d
872872
def _validate_log_format(log) -> bool:
873873
return all(key in log for key in ('started_at', 'ended_at', 'result'))
874874

875+
# A custom print function that sets `flush=True`` by default to ensure immediate output to stdout, useful when redirecting output to a file.
876+
def print(*args, **kwargs):
877+
kwargs.setdefault("flush", True)
878+
builtins.print(*args, **kwargs)
879+
875880
def result_is_success(result) -> bool:
876881
if not result or (isinstance(result, requests.Response) and result.status_code != 200):
877882
return False

0 commit comments

Comments
 (0)