简体   繁体   中英

Python Checkout Simulation

I am trying to create a program that simulates a checkout at a store. I allow users to input a bunch of data and then I pass them into my function that runs the simulation. I am using a Queue class that I have created and works for what I need it for (FIFO). My problem comes when I am trying to get client that enter the queue out to an open server and then eventually removed from the queue. Once they are gone, the server will select the next client from the queue and so on.

Below is my code of what I have so far..

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)

So this code here calls upon my Queue class aswell as some other small classes I made. Im not including them because I don't think I need to include them for what I need help with.

My question is, how can I get the servers that I have put into a list to take out a client out of the queue one at a time each, and then once they are done being served, remove them from the queue and take the next one.

I hope I was clear, please comment if you need more clarification.

As you haven't posted your queue code, it's hard to be certain, but you should remove the customer from the queue as soon as the server is ready for them, to prevent multiple servers being assigned the same customer:

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM