Skip to content

Commit facb286

Browse files
author
Nick Tsai
committed
tmp
1 parent 8e0a1d8 commit facb286

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "worker_dispatcher"
7-
version = "0.0.1"
7+
version = "0.0.2"
88
authors = [
99
{ name="Nick Tsai", email="myintaer@gmail.com" },
1010
]

src/worker_dispatcher/worker_dispatcher.py

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import copy
2-
import queue
3-
import threading
4-
import multiprocessing
5-
import concurrent.futures
6-
import time
1+
import multiprocessing, concurrent.futures
2+
import time, copy
73

4+
# Sample callback function
85
def callback_sample(config, task_id: int, task_data):
96
if task_id == 1:
107
print("Runing sample task function, please customize yours according to the actual usage.")
@@ -13,11 +10,13 @@ def callback_sample(config, task_id: int, task_data):
1310
}
1411
return result
1512

13+
# Sample result callback function
1614
def result_callback_sample(config, task_id: int, task_result, log):
1715
if task_id == 1:
1816
print("Runing sample result function, please customize yours according to the actual usage.")
1917
return task_result
2018

19+
# Default configuration
2120
default_config = {
2221
'debug': False,
2322
'task': {
@@ -27,22 +26,16 @@ def result_callback_sample(config, task_id: int, task_result, log):
2726
'result_callback': False
2827
},
2928
'worker': {
30-
'number': 1,
29+
'number': multiprocessing.cpu_count(),
3130
'per_second': 0,
32-
'multiprocessing': False,
33-
},
34-
'output': {
35-
'enabled': False,
36-
'format': 'csv',
37-
'filepath': './',
38-
'filename': 'output.csv',
39-
'header': [],
31+
'multiprocessing': False
4032
}
4133
}
4234

4335
results = []
4436
logs = []
4537

38+
# Start function
4639
def start(userConfig: dict):
4740

4841
global results, logs
@@ -64,10 +57,10 @@ def start(userConfig: dict):
6457
in_child_process = (multiprocessing.current_process().name != 'MainProcess')
6558
# Return False if is in worker process to let caller handle
6659
if in_child_process:
67-
print("Exit procedure due to the child process")
60+
# print("Exit procedure due to the child process")
6861
return False
6962

70-
63+
# Debug mode
7164
if config['debug']:
7265
print("Configuration:")
7366
print(config)
@@ -77,7 +70,6 @@ def start(userConfig: dict):
7770
exit("Callback function is invalied")
7871

7972
# Task list to queue
80-
task_queue = multiprocessing.Queue() if use_multiprocessing else queue.Queue()
8173
task_list = []
8274
user_task_list = config['task']['list']
8375
if isinstance(user_task_list, list):
@@ -101,19 +93,22 @@ def start(userConfig: dict):
10193
# Worker dispatch
10294
worker_num = config['worker']['number']
10395
worker_num = worker_num if isinstance(worker_num, int) else 1
96+
worker_per_second = config['worker']['per_second'] if config['worker']['per_second'] else 0
97+
max_workers = len(task_list) if worker_per_second else worker_num
10498
# result_queue = multiprocessing.Queue() if use_multiprocessing else queue.Queue()
10599
pool_executor_class = concurrent.futures.ProcessPoolExecutor if use_multiprocessing else concurrent.futures.ThreadPoolExecutor
106100
print("Start to dispatch workers ---\n")
107101

108102
# Pool Executor
109-
with pool_executor_class(max_workers=worker_num) as executor:
103+
with pool_executor_class(max_workers=max_workers) as executor:
110104
pool_results = []
105+
# Task dispatch
111106
for task in task_list:
112107
pool_result = executor.submit(consume_task, task, config)
113108
pool_results.append(pool_result)
114109
# Worker per_second setting
115-
if config['worker']['per_second'] and task['id'] % worker_num == 0:
116-
time.sleep(float(config['worker']['per_second']))
110+
if worker_per_second and task['id'] % worker_num == 0:
111+
time.sleep(float(worker_per_second))
117112
# Get results from the async results
118113
for pool_result in concurrent.futures.as_completed(pool_results):
119114
log = pool_result.result()
@@ -127,20 +122,23 @@ def start(userConfig: dict):
127122
print("End of worker dispatch ---\n")
128123
return results
129124

130-
def get_results():
131-
return results
132-
133-
def get_logs():
134-
return logs
135-
125+
# Worker function
136126
def consume_task(data, config):
137127
started_at = time.time()
138128
return_value = config['task']['callback'](config['task']['config'], data['id'], data['task'])
139129
ended_at = time.time()
130+
duration = ended_at - started_at
140131
log = {
141132
'task_id': data['id'],
142133
'started_at': started_at,
143134
'ended_at': ended_at,
135+
'duration': duration,
144136
'result': return_value
145137
}
146-
return log
138+
return log
139+
140+
def get_results():
141+
return results
142+
143+
def get_logs():
144+
return logs

0 commit comments

Comments
 (0)