[Python] simple example of Threadpool job termination, pythonthreadpool
Requirement
We need to process a string of single digits (0 ~ 9) when the odd number is used, print it cyclically. If the even number is used, wait for the response time and complete all tasks. If the value is 0, it is an error, but the task does not need to be terminated. You can customize some processing.
Key Points
Define func function processing requirements
The callback process returns only the even number and 0 number. The odd number will always be executed. To control the status of the thread pool, an exception must be thrown for the even number and 0 number, and exception handling must be captured.
Threadpool defines thread pool concurrency
Implementation
# -*- coding: utf-8 -*-
from threadpool import makeRequests, ThreadPool
import time
from multiprocessing import Process
Exception definition and special value (0) Definition
class Finish(SyntaxWarning):
pass
class PauseInfo(SyntaxWarning):
pass
pause_num = 0
Func Function Definition
If the value is 0, False is returned. If the value is an even number, True is returned.
def func(para):
if para == pause_num:
print('start for %d and wait %ds' % (para, 4))
time.sleep(4)
print('error bcs ',para)
return False
if para % 2 == 0:
print('start for %d and wait %ds' % (para, para))
time.sleep(para)
print('stop for', para)
return True
while True:
print('continue for', para)
time.sleep(para)
Callback Definition
def callback(request, result):
if result:
raise Finish
else:
raise PauseInfo
Thread Pool Processing
Finish indicates that the task is completed and an exception is thrown again to exit the thread pool for processing;
def main_thread(paras):
pool = ThreadPool(10)
requests = makeRequests(callable_=func, args_list=paras, callback=callback)
[pool.putRequest(req) for req in requests]
while True:
try:
pool.wait()
except Finish as e:
raise SystemExit
except PauseInfo as e:
print('Pause bcs %d but will continue' % pause_num)
except Exception as e:
print('Unknown error so will quit')
raise SystemExit
The main function starts a test process.
if __name__ == '__main__':
while True:
s = input('Input number list to test and any other word to quit\n')
paras = []
for para in s:
if para.isnumeric():
paras.append(int(para))
else:
break
try:
thread_test = Process(target=main_thread, args=(paras,))
thread_test.start()
thread_test.join(timeout=20)
except TimeoutError as e:
print('task timeout')
except Exception as e:
print('unknow error:',e)
Result Verification
Process 108. You can see that 1 is processed cyclically, 0 is returned when processing, and 8 is finished after processing.
Input number list to test and any other word to quit
108
Continue for 1
Start for 0 and wait 4S
Start for 8 and wait 8 s
Continue for 1
Continue for 1
Continue for 1
Error bcs 0
Continue for 1
Pause bcs 0 but will continue
Continue for 1
Continue for 1
Continue for 1
Continue for 1
Stop for 8
Input number list to test and any other word to quit