简体   繁体   English

python 如何while循环while true,一起运行多个函数

[英]python How to while loop while true , run multiple functions together

def function():
    while True:  
        ...omission...(this function is repeated permanently)
i =0
while i < 4:
    driver.execute_script("""window.open("URL")""")
    driver.switch_to.window(driver.window_handles[-1])
    time.sleep(1)
    function()
    time.sleep(1)
    i += 1  #open new tab and run function.

it doesn't work because while true loop is repeated permanently.它不起作用,因为 while true 循环永久重复。 Is there any ways to run multiple functions together?有没有办法同时运行多个功能? https://imgur.com/a/4SIVekS This picture shows what I want https://imgur.com/a/4SIVekS这张图是我想要的

I don't understand your question because I don't understand what your function is supposed to do.我不明白你的问题,因为我不明白你的 function 应该做什么。

while True:

will always create an infinite loop.将始终创建一个无限循环。 "while" is a command that tells python to loop through the following block so long as the expression following it evaluates to True. “while”是一个命令,它告诉 python 循环通过以下块,只要它后面的表达式计算为 True。 True always evaluates to True. True 总是计算为 True。

It seems like you want to use a conditional, like you do in "while x < 4".似乎您想使用条件,就像您在“while x < 4”中所做的那样。

x < 4

...is an expression that evaluates to true when x is less than 4, and false if x is not less than 4. Everything below the line: ...是一个表达式,当 x 小于 4 时计算结果为 true,如果 x 不小于 4,则计算结果为 false。行以下的所有内容:

while x < 4:

will then run if x is less than 4, and when it's done running that code, it will go back and evaluate if x is less than 4 again, and if it is, run the code again.如果 x 小于 4 则将运行,当它运行完该代码时,它将返回 go 并再次评估 x 是否小于 4,如果是,则再次运行代码。 To include another while loop inside of that loop, that new loop also needs an expression to evaluate.要在该循环中包含另一个 while 循环,该新循环还需要一个表达式来评估。 If you want to evaluate the same expression, write it out:如果要计算相同的表达式,请将其写出:

while x < 4:
    # do something
    while x < 4:
        #do more things
    # do even more things
    # change x at some point, or else you're in an infinite loop.

However, there's no reason to do that specifically, because you're already doing it.但是,没有理由专门这样做,因为您已经在这样做了。 All of the code is only running when x < 4, so checking that condition again right there is redundant, and doing it in another loop doesn't make sense.所有代码仅在 x < 4 时运行,因此再次检查该条件是多余的,并且在另一个循环中执行它是没有意义的。 If the inside loop is also incrementing x, then the outside loop won't loop and doesn't need to increment x.如果内部循环也在增加 x,那么外部循环将不会循环并且不需要增加 x。

Also, if you want a function to check a condition based on a variable outside the function, you'll want to pass things to that function.此外,如果您希望 function 根据 function 之外的变量检查条件,则需要将内容传递给该 function。

According to your picture, what you want is to launch the function a set number of times (4?), and run those in parrallel.根据您的图片,您想要启动 function 一定次数(4?),然后并行运行。 On a single core, as is the normal behavior, straight up parallel processing is impossible.在单核上,与正常行为一样,直接并行处理是不可能的。 You need to access other cores and manage a decentralized processing.您需要访问其他核心并管理分散的处理。 while is useless there. while在那里没用。 I'm worried the level of difficulty is over your current skills, but here we go.我担心难度级别超过了你目前的技能,但这里我们 go。

The overall flow that you (probably, depends on the actual memory safety of your functions) need is:您(可能取决于您的功能的实际 memory 安全性)需要的总体流程是:
- to create a thread pool with the set number of threads for the number of runs you want. - 为您想要的运行次数创建具有设定线程数的线程池。
- indicate the function you need to run - 指明你需要运行的 function
- start them, making sure the start itself is non-blocking. - 启动它们,确保启动本身是非阻塞的。
- ensure one functions's processing doesn't impact another's results. - 确保一个函数的处理不会影响另一个函数的结果。 race conditions are a common problem.竞争条件是一个常见问题。
- gather results, again, in a non-blocking way. - 再次以非阻塞方式收集结果。

You can use several methods.您可以使用多种方法。 I highly recommend you read up a lot on the following documentations.我强烈建议您阅读以下文档。

Threading:穿线:
https://docs.python.org/3/library/threading.html https://docs.python.org/3/library/threading.html

Multiprocessing:多处理:
https://docs.python.org/3/library/multiprocessing.html https://docs.python.org/3/library/multiprocessing.html

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

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