ใช้โหมดผู้ผลิตผู้ผลิตอย่างสะดวกใช้มัลติเธรด + คิวเพื่อรองรับโหมดผู้ผลิตหลายโหมดหลายโหมดเพื่อรองรับโหมดคิวการบริโภคลำดับความสำคัญเพื่อรองรับโหมดการ จำกัด สตรีม
pip install thread_worker
Worker ทั่วไปทำงานหลายเธรด
คิวการบริโภคลำดับความสำคัญ PriorWorker
งาน LimitWorker การ จำกัด งานปัจจุบัน
การเริ่มต้นพารามิเตอร์ต่อไปนี้พร้อมใช้งาน
def __init__ ( self , consumer_func , consumer_count , logger , block , timeout ):
'''
@params consumer_func consumer_func default None
@params consumer_count Work quantity
@params logger You can customize the logger. The default is logging
@params block You can set whether the thread ends when the queue is empty
By default, the thread will end when the queue is empty and the thread task is completed
You can set it to true to enable work to monitor tasks all the time
@params timeout The timeout for obtaining data in the queue is 5S by default, that is, if the data is not obtained within 5S, it will end
''' ฟังก์ชั่น Consumer_Func ของผู้บริโภคเริ่มต้นที่ไม่มี หากเพิ่มเข้ามาถือว่าผู้บริโภคเริ่มต้นก่อนและจากนั้นงานจะถูกสร้างขึ้น Consumer_count ควบคุมจำนวนผู้บริโภคที่นั่น
Logger คือการตั้งค่าเอาต์พุตบันทึก
บล็อกคือการควบคุมว่าเป็นโหมดการบล็อกหรือไม่ หากตั้งค่าเป็นจริงเธรดการทำงานจะดำเนินการต่อไปจนกว่ากระบวนการหลักจะสิ้นสุดลง มิฉะนั้นเมื่อคิวว่างเปล่าและงานเธรดเสร็จสมบูรณ์งานจะสิ้นสุดลง
การหมดเวลาคือการหมดเวลาสำหรับการได้รับข้อมูลจากคิว การหมดเวลาเริ่มต้นคือ 5s หากการเข้าร่วมเป็นจริงงานจะเข้าร่วมเท็จอีกครั้งและสิ้นสุดงานปัจจุบัน
หนึ่งคือการเรียกใช้ผู้บริโภคก่อนจากนั้นสร้างมันขึ้นมา มันสามารถสร้างและบริโภคอย่างต่อเนื่อง เหมาะสำหรับงานสแกนตัวแทนแบบพาสซีฟหรือกระบวนการหลักจะควบคุมทั่วโลกแล้วสร้างงาน
หนึ่งคือการสร้างงานทั้งหมดในครั้งเดียวและส่งมอบให้กับผู้บริโภคเพื่อเรียกใช้งานที่เหมาะสำหรับการสร้างการทดสอบในครั้งเดียวแล้วเรียกใช้
WorkWork put ข้อมูลของอินสแตนซ์Work.is_end() from thread_worker import Worker
import requests
import time
def consumer ( iid ):
url = "https://www.baidu.com/{0}" . format ( iid )
resp = requests . get ( url = url )
print ( resp . request . url )
# 不需要阻塞
w = Worker ( consumer , consumer_count = 1 , block = False )
for iid in range ( 10 ):
w . put ( iid )
# 这里通过 is_end 方法来阻塞程序
while not w . is_end ():
time . sleep ( 3 ) เช่นเดียวกับงานเริ่มต้นมีพารามิเตอร์ limit_time เพิ่มเติมเมื่อสร้าง โดยการตั้งค่าเวลา limit_time และ consumer_count มันจะควบคุมจำนวนงานที่จะดำเนินการภายในเวลาหน่วย
ค่าเริ่มต้น limit_time คือ 1s
from thread_worker import LimitWorker
import requests
import time
def consumer ( iid ):
url = "https://www.baidu.com/{0}" . format ( iid )
resp = requests . get ( url = url )
print ( resp . request . url )
# limit_time 是limit_time 秒内有 consumer_count个消费者
w = LimitWorker ( consumer , consumer_count = 1 , block = False , limit_time = 3 )
for iid in range ( 10 ):
w . put ( iid )
# 这里通过 is_end 方法来阻塞程序
while not w . is_end ():
time . sleep ( 3 )ผลลัพธ์เป็นเช่นนี้ จะมีการส่งคำขอเท่านั้นทุก 3 วินาทีนั่นคือความถี่จะถูกควบคุม
send: 2022-04-08 10:13:04 904000
https://www.baidu.com/0
send: 2022-04-08 10:13:07 904000
https://www.baidu.com/1
send: 2022-04-08 10:13:10 904000
https://www.baidu.com/2
send: 2022-04-08 10:13:13 904000
https://www.baidu.com/3
send: 2022-04-08 10:13:16 904000
https://www.baidu.com/4
send: 2022-04-08 10:13:19 904000
https://www.baidu.com/5
send: 2022-04-08 10:13:22 904000
https://www.baidu.com/6
send: 2022-04-08 10:13:25 904000
https://www.baidu.com/7
send: 2022-04-08 10:13:28 904000
https://www.baidu.com/8
send: 2022-04-08 10:13:31 904000
https://www.baidu.com/9
ในสถานการณ์นี้ตัวแทนแฝงหรือกระบวนการหลักบางอย่างจะถูกนำมาใช้ในโปรแกรมที่ทำงานเป็นเวลานาน ความแตกต่างคือไม่จำเป็นต้องใช้วิธีการทำงาน is_end () เพื่อบล็อกบล็อกหรือตั้งบล็อก ค่าเริ่มต้นเป็นจริง
from thread_worker import Worker
import requests
import time
def consumer ( iid ):
url = "https://www.baidu.com/{0}" . format ( iid )
resp = requests . get ( url = url )
print ( resp . request . url )
# block默认就是True的
w = Worker ( consumer , consumer_count = 1 )
for iid in range ( 10 ):
w . put ( iid )
# 手动阻塞
while True :
pass