我正在尝试创建一个模拟商店结账的程序。我允许用户输入一堆数据,然后将它们传递给运行模拟的函数。我正在使用我创建的Queue类,并为我需要它(FIFO)工作。当我试图让客户端进入打开的服务器然后最终从队列中删除时,我的问题出现了。一旦它们消失,服务器将从队列中选择下一个客户端,依此类推。
以下是我目前所拥有的代码..
from queue_array import Queue
from customer import Customer
from server import Server
import random, copy
def line_up (total_time, num_servers, max_num_clients_per_min, max_service, min_service, freakout_chance, min_freak, max_freak):
q = Queue()
s = []
customer_id = 0
server_id = 0
max_queue_size = 0
serving = 0
customers_served = 0
is_serving = False
for _se in range(num_servers):
server = Server(server_id, None)
s.append(server)
server_id += 1
#loop through each minute
for time in range(0, total_time + 1):
print ("Time = {}".format(time))
num_join = random.randint(0, max_num_clients_per_min)
#Determine when customer joins the queue
for _i in range(0, num_join):
print (" Client {} joins the queue.".format(customer_id))
service_length = random.randint(min_service, max_service)
f = random.randint(1, freakout_chance)
if f == 1:
freak = True
else:
freak = False
customer = Customer(customer_id, copy.deepcopy(time), service_length, freak)
q.insert(customer)
customer_id += 1
if len(q) > max_queue_size:
max_queue_size = len(q)
for serve in s:
if serve.client is None:
n = serve.number
print (" Server {} starting to serve Client {}".format(n, q.peek().number))
line_up(5, 3, 2, 5, 1, 1000, 0, 5)
所以这里的代码调用了我的Queue类以及我制作的其他一些小类。我不包括他们,因为我不认为我需要将它们包含在我需要帮助的地方。
我的问题是,如何将我放入列表中的服务器逐个从客户端中取出一个客户端,然后在完成服务后,将它们从队列中删除,拿下一个。
我希望我很清楚,如果您需要更多说明,请发表评论。
答案 0 :(得分:1)
由于您尚未发布您的队列代码,因此很难确定,但您应该在服务器准备就绪后立即从队列中删除客户,以防止为同一客户分配多个服务器:
for serve in s:
if serve.client is None:
serve.client = q.remove() # or pop or whatever you have
print (" Server {} starting to serve Client {}".format(serve.number, serve.client.number))