【python】Threadpool線程池任務終止簡單樣本,pythonthreadpool

來源:互聯網
上載者:User

【python】Threadpool線程池任務終止簡單樣本,pythonthreadpool
需求

加入我們需要處理一串個位元(0~9),奇數時需要迴圈列印它;偶數則等待對應時間長度並完成所有任務;0則是錯誤,但不需要終止任務,可以自訂一些處理。

 

關鍵點

定義func函數處理需求

callback處理返回結果,只有偶數和0返回;奇數會一直執行;要控制線程池狀態,則需要針對偶數和0時拋出異常,並捕獲異常處理。

threadpool定義線程池並發

 

實現
# -*- coding: utf-8 -*-
from threadpool import makeRequests, ThreadPool
import time
from multiprocessing import Process
異常定義和特殊值(0)定義
class Finish(SyntaxWarning):
pass



class PauseInfo(SyntaxWarning):
pass


pause_num = 0
 
func函數定義

0時返回False,其他偶數返回True

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定義
def callback(request, result):
if result:
raise Finish
else:
raise PauseInfo
 
線程池處理

Finish標識任務完成,再次誘發異常退出線程池處理;

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
 
主函數起一個測試進程
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)
 
結果驗證

 

處理108,看列印可以看到,1被迴圈處理,0處理的時候報錯;8處理完畢則任務結束

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 8s
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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.