简体   繁体   English

如何在python中同时运行多个循环函数?

[英]How to run several looping functions at the same time in python?

i've been working on a project that i was assigned to do. 我一直在从事我被分配做的项目。 it is about some sort of parking lot where the cars that enter, are generated automaticly (done) now, I've put them into a 'waiting list (because i have to represent them with a GUI module later) in order to later be assigned in a spot in the parking lot. 它是关于某种停车场的,进入的汽车现在自动(完成)生成,我将它们放入“等待列表”(因为稍后我必须用GUI模块表示),以便稍后在停车场的一个地点分配的。 and then they must get out the parking lot (also randomly) 然后他们必须离开停车场(也是随机的)

The problem raises when I created a function that will always create cars randomly, now i cant call any other function because the first one is looping. 当我创建一个将始终随机创建汽车的函数时,问题就出现了,现在我无法调用任何其他函数,因为第一个函数正在循环。

the question is, is there a way to call several looping functions at the same time? 问题是,有没有一种方法可以同时调用多个循环函数?

Thanks 谢谢

the question is, is there a way to call several looping functions at the same time? 问题是,有没有一种方法可以同时调用多个循环函数?

This is a great question and there are several ways to do it. 这是一个很大的问题,有几种方法可以解决。

Threading can let your functions run concurrently. 线程可以让您的函数并发运行。 The data flow between the threads should be managed using the Queue module : 线程之间的数据流应使用Queue模块进行管理:

# Inter-thread communication
wait_to_park = Queue()
wait_to_exit = Queue()

# Start the simulation
tg = threading.Thread(target=generate_cars)
tp = threading.Thread(target=park_cars)
tu = threading.Thread(target=unpark_cars)
tg.start(); tp.start(); tu.start()

# Wait for simumlation to finish
tg.join()
wait_to_park.join()
tp.join()
wait_to_exit.join()
tu.join()

Alternatively, you can use an event-loop such as the sched module to coordinate the events. 另外,您可以使用事件循环(例如sched模块)来协调事件。 Generators may help with this -- they work like functions that can be suspended and restarted. 生成器可能会为此提供帮助-它们像可以暂停和重新启动的函数一样工作。

maybe import random and then set up a range that you want certain events to happen? 也许导入random ,然后设置一个范围,您希望某些事件发生?

def mainLoop():
   while True:
      x = random.randrange(1,100)
      if 0>x>10: do something()
      if 10>x>60: do somethingMoreFrequently()
      if 60>x>61: do somethingRarely()

etcetera 诸如此类

if you LITERALLY want to call several looping functions at the same time be prepared to learn about Threading. 如果您想同时调用多个循环函数,请准备好学习线程。 Threading is difficult and i never do it unless 100% necessary. 线程化很困难,除非100%必要,否则我绝不会这样做。

but this should be simple enough to achieve without 但这应该足够简单,而无需

Don't have both infinitely loop, have then each do work if needed and return (or possibly yield). 不要同时具有两个无限循环,然后在需要时每个都可以工作并返回(或可能产生)。 Then have your main event loop call both. 然后让您的主事件循环都调用。 Something like this: 像这样:

def car_arrival():
    if need_to_generate_car:
        # do car generation stuff
    return

def car_departure()
   if time_for_car_to_leave:
       # do car leaving stuff
   return


def event_loop():
    while sim_running:
        car_arrival()
        car_departure()
        sleep(0.5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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