แนะนำตัว..
ในตัวอย่างนี้ เราจะสร้างคิวงานที่เก็บงานทั้งหมดที่ต้องดำเนินการและกลุ่มเธรดที่โต้ตอบกับคิวเพื่อประมวลผลองค์ประกอบทีละรายการ
เราจะเริ่มด้วยคำถามว่า Queue คืออะไร? คิวคือโครงสร้างข้อมูลที่รวบรวมองค์ประกอบต่างๆ ไว้ในลำดับที่เฉพาะเจาะจงมาก ให้ฉันอธิบายโดยยกตัวอย่างชีวิตจริง
สมมติว่าคุณยืนต่อแถวเพื่อจ่ายเงินที่เคาน์เตอร์ร้านขายของชำ (อย่าถามฉันว่าร้านไหน)
ในแถวรอจ่ายบิล คุณจะสังเกตเห็นสิ่งต่อไปนี้:
1. ผู้คนเข้าที่ปลายสายด้านหนึ่งและออกจากปลายอีกด้านหนึ่ง
2. ถ้าคน A เข้าแถวก่อนคน B คน A จะออกจากแถวก่อนคน B (เว้นแต่คน B เป็นคนดังหรือมีลำดับความสำคัญมากกว่า)
3. เมื่อทุกคนชำระบิลแล้ว จะไม่มีใครอยู่ในสาย
กลับไปที่การเขียนโปรแกรมที่คิวทำงานในลักษณะเดียวกัน
1. enqueue - เพิ่มองค์ประกอบต่อท้ายคิว
2. dequeue - องค์ประกอบถูกลบออกจากจุดเริ่มต้นของคิว
ยังมีอีกมาก เข้าก่อนออกก่อน (FIFO) - องค์ประกอบที่เพิ่มเข้ามาก่อนจะถูกลบออกก่อน เข้าก่อนออกก่อน (LIFO) - องค์ประกอบสุดท้ายที่เพิ่มจะถูกลบออกก่อน
Python ใช้โครงสร้างข้อมูล Queue อย่างไร
โมดูลคิวใน Python จัดเตรียมการใช้งานโครงสร้างข้อมูลคิวอย่างง่าย แต่ละคิวสามารถมีวิธีการดังต่อไปนี้
-
get():คืนค่าองค์ประกอบถัดไป
-
put():เพิ่มองค์ประกอบใหม่
-
qsize():จำนวนองค์ประกอบปัจจุบันในคิว
-
empty():คืนค่าบูลีนเพื่อระบุว่าคิวว่างหรือไม่
-
full():คืนค่าบูลีนซึ่งระบุว่าคิวเต็มหรือไม่
1. เราจะสร้างฟังก์ชันที่รับอาร์กิวเมนต์ x แล้ววนซ้ำผ่านตัวเลขระหว่าง 1 กับตัวมันเอง (x) เพื่อทำการคูณ สำหรับเช่น เมื่อคุณส่ง 5 ไปยังฟังก์ชันนี้ มันจะวนซ้ำผ่าน 1 ถึง 5 และคูณต่อไป เช่น 1 คูณ 5, 2 คูณ 5, 3 คูณ 5, 4 คูณ 5, 5 คูณ 5 ในที่สุดก็คืนค่าเป็นรายการ
ตัวอย่าง
def print_multiply(x):
output_value = []
for i in range(1, x + 1):
output_value.append(i * x)
print(f"Output \n *** The multiplication result for the {x} is - {output_value}")
print_multiply(5) ผลลัพธ์
*** The multiplication result for the 5 is - [5, 10, 15, 20, 25]
2. เราจะเขียนฟังก์ชันอื่นที่เรียกว่า process_queue() ซึ่งจะพยายามรับองค์ประกอบถัดไปของอ็อบเจกต์คิว ตรรกะสำหรับสิ่งนี้ค่อนข้างง่าย ให้ผ่านองค์ประกอบต่อไปจนกว่าคิวจะว่างเปล่า ฉันจะใช้โหมดสลีปเพื่อชะลอการดำเนินการเล็กน้อย
ตัวอย่าง
def process_queue(): while True: try: value = my_queue.get(block=False) except queue.Empty: return else: print_multiply(value) time.sleep(2)
3.สร้างคลาส เมื่อมีการเริ่มต้นและเริ่มต้นอินสแตนซ์ใหม่ ฟังก์ชัน process_queue() จะถูกเรียก
ตัวอย่าง
class MultiThread(threading.Thread):
def __init__(self, name):
threading.Thread.__init__(self)
self.name = name
def run(self):
print(f" ** Starting the thread - {self.name}")
process_queue()
print(f" ** Completed the thread - {self.name}") 4.สุดท้ายเราจะผ่านรายการป้อนตัวเลขและกรอกคิว
# setting up variables input_values = [5, 10, 15, 20] # fill the queue my_queue = queue.Queue() for x in input_values: my_queue.put(x)
5.สุดท้ายก็จัดมาให้หมด
import queue
import threading
import time
# Class
class MultiThread(threading.Thread):
def __init__(self, name):
threading.Thread.__init__(self)
self.name = name
def run(self):
print(f"Output \n ** Starting the thread - {self.name}")
process_queue()
print(f" ** Completed the thread - {self.name}")
# Process thr queue
def process_queue():
while True:
try:
value = my_queue.get(block=False)
except queue.Empty:
return
else:
print_multiply(value)
time.sleep(2)
# function to multiply
def print_multiply(x):
output_value = []
for i in range(1, x + 1):
output_value.append(i * x)
print(f" \n *** The multiplication result for the {x} is - {output_value}")
# Input variables
input_values = [2, 4, 6, 5,10,3]
# fill the queue
my_queue = queue.Queue()
for x in input_values:
my_queue.put(x)
# initializing and starting 3 threads
thread1 = MultiThread('First')
thread2 = MultiThread('Second')
thread3 = MultiThread('Third')
thread4 = MultiThread('Fourth')
# Start the threads
thread1.start()
thread2.start()
thread3.start()
thread4.start()
# Join the threads
thread1.join()
thread2.join()
thread3.join()
thread4.join() ผลลัพธ์
** Starting the thread - First *** The multiplication result for the 2 is - [2, 4]
ผลลัพธ์
** Starting the thread - Second *** The multiplication result for the 4 is - [4, 8, 12, 16]
ผลลัพธ์
** Starting the thread - Third *** The multiplication result for the 6 is - [6, 12, 18, 24, 30, 36]
ผลลัพธ์
** Starting the thread - Fourth *** The multiplication result for the 5 is - [5, 10, 15, 20, 25] *** The multiplication result for the 10 is - [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] *** The multiplication result for the 3 is - [3, 6, 9] ** Completed the thread - Third ** Completed the thread - Fourth ** Completed the thread - Second ** Completed the thread - First
6.เราได้นำแนวคิดเกี่ยวกับคิวมาใช้สำเร็จแล้ว ดูสิ เรามี 4 เธรดแต่มี 6 ค่าที่ต้องดำเนินการ ดังนั้นใครก็ตามที่มาถึงคิวก่อนจะถูกดำเนินการ และคนอื่นๆ จะรอดำเนินการให้เสร็จสิ้น
คล้ายกับชีวิตจริง สมมติว่ามี 3 เคาน์เตอร์ แต่มีคน 10 คนรอจ่ายบิล ดังนั้น 10 คนจะอยู่ใน 3 คิว และใครที่เคยชำระบิลเสร็จจะออกจากแถวหาคนต่อไป
พี>