添加队列和锁



你好,我开始研究这段代码,这段代码是关于红绿灯的问题。一个让汽车向右行驶,另一个让它们向下/直行。问题是,我想添加队列,但也添加一些锁,以确保一切正常。

from re import T
import threading
import random
import time
import concurrent.futures
import traceback
x_squares = 5
y_squares = 5
coordinate_dictionary = {}
for x in range(0,x_squares + 1):
for y in range(0,y_squares + 1):
coordinate_dictionary[(x,y)] = {
"right_queue": [],
"down_queue": [],
"where_to_go": "right" if x % 3 == 0 else "down",
"right_queue_lock": threading.Lock(),
"down_queue_lock": threading.Lock(),
"x": x,
"y": y
}

def semaphore(structure):
while is_the_program_over == False:
print(f"The semaphor x: {structure['x']} y: {structure['y']} changing colors ")
if structure["where_to_go"] == "right":
structure["where_to_go"] = "down"
else:
structure["where_to_go"] = "right"
time.sleep(random.randrange(3,7))

def cars(id):
x,y = 0,0
route = ""
direction = random.randrange(1,3)
if direction == 1:
route = "down"
else:
route = "right"
print(f"Car {id} is going on route: {route}")
while x <= x_squares and y <= y_squares:
print(f"Car {id} is moving from {x},{y}")
if route == "down":
dictionary = coordinate_dictionary[(x,y)]
while dictionary["where_to_go"] != "down":
time.sleep(1)
y = y+1
if route == "right":
dictionary = coordinate_dictionary[(x,y)]
while dictionary["where_to_go"] != "right":
time.sleep(1)
x = x+1
print(f"Car {id} arrived to {x},{y}")
time.sleep(1.5)

is_the_program_over = False
amount_of_cars = 25
amount_of_semaphors = len(coordinate_dictionary.items())
with concurrent.futures.ThreadPoolExecutor(max_workers=amount_of_semaphors) as executor:
executor.map(semaphore, coordinate_dictionary.values())
with concurrent.futures.ThreadPoolExecutor(max_workers=amount_of_cars) as executor:
executor.map(cars, range(amount_of_cars))
is_the_program_over = True

我建议使用threading.Condition来处理在每个红绿灯处排队的车辆。每个红绿灯一个条件变量就足够了,我们实际上不需要每个方向或队列都有一个。

# setup code
for x in range(0,x_squares + 1):
for y in range(0,y_squares + 1):
coordinate_dictionary[(x,y)] = {
"where_to_go": "right" if x % 3 == 0 else "down",
"cv": threading.Condition(),
"x": x,
"y": y
}
# logic for the stoplight
def semaphore(structure):
condition = structure["cv"]
while is_the_program_over == False:
print(f"The semaphor x: {structure['x']} y: {structure['y']} changing colors ")
with condition:                               # acquire the lock
if structure["where_to_go"] == "right":   # change the direction
structure["where_to_go"] = "down"
else:
structure["where_to_go"] = "right"
condition.notify_all()                    # wake up any waiting cars
time.sleep(random.randrange(3,7))
# logic for the cars
def cars(id):
x,y = 0,0
route = ""
direction = random.randrange(1,3)
if direction == 1:
route = "down"
else:
route = "right"
print(f"Car {id} is going on route: {route}")
while x <= x_squares and y <= y_squares:
print(f"Car {id} is moving from {x},{y}")
if route == "down":
dictionary = coordinate_dictionary[(x,y)]
cv = dictionary["cv"]
with cv:                                                     # acquire lock
cv.wait_for(lambda: dictionary["where_to_go"] == "down") # wait if needed
y = y+1
if route == "right":
dictionary = coordinate_dictionary[(x,y)]

cv = dictionary["cv"]
with cv:                                                     # same here
cv.wait_for(lambda: dictionary["where_to_go"] == "right") 
x = x+1
print(f"Car {id} arrived to {x},{y}")
time.sleep(1.5)

最新更新