在这种情况下,IF语句或多处理是否正确使用? [英] Would an IF statement or multiprocessing be correct to use in this instance?

查看:51
本文介绍了在这种情况下,IF语句或多处理是否正确使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

addNewGreeting():
        protocol = input()
        protocol = protocol.upper()
        protocol1_list = ["PROTOCOL 1", "PROTOCOL ONE", "INITIATE NEW GREETING" ]
        for i in protocol1_list:
            if i in protocol:
                rest of function

 def removeOldGreeting():
        protocol = input("")
        protocol = protocol.upper()
        protocol2_list = ["PROTOCOL 2", "PROTOCOL TWO", "INITIATE DELETE GREETING"]
        for i in protocol2_list:
            if i in protocol:
                rest of function





我如何让这两个函数一起运行,等待输入,当输入其中一个函数的一个列表的输入时,只运行该函数而另一个停止?



我读过关于多处理的事情,我在全球范围内使用了if语句,但我意识到会有很多elif,因为我想检查几十,如果不是数百个功能。如果它在函数中,我会发现它更容易阅读但是在计算机上同时运行那么多函数可能很难。对不起,如果这很难理解,我不太擅长解释事情。



我尝试了什么:



我试图在函数之外使用if语句,但我发现必须有多个elif。



How would I make these two functions run together, wait for an input and when an input from one of the lists from one of the functions is typed in, only that function is ran and the other stops?

I've read about multiprocessing and I've used an if statement globally but I've realized that there will be many elif's because I want to check tens, if not hundreds of functions. I would just find it easier to read if it was in the function but then again it might be hard on the computer to run that many functions at the same time. Sorry if this is difficult to understand, I'm not very good at explaining things.

What I have tried:

I have tried to use if statements outside of the functions but I have found that there will have to be multiple elif's.

推荐答案

我没有得到你真正愿意做的事。但是形成这些行,由你说,
I don't get what you are actually willing to do. But form these lines said by you ,
"How would I make these two functions run together"

。我知道你愿意在你的应用程序中实现线程。



这里是你如何实现两个函数的线程,并将两个函数一起运行。[例子] < br $> b $ b

. I understand you are willing to implement Threading in your application.

Here is how you implement thread for two functions and will make the two functions run together.[Example]

#Python version 3.5.2
from threading import Thread

def function1():
    for i in range(100):
        print("Function 1 is executing")

def function2():
    for i in range(100):
        print("Function 2 is executing")

t = Thread(target=function1)
t.start()
t1 = Thread(target=function2)
t1.start()





和输出将是这样的



and the output will be like this

Function 2 is executingFunction 1 is executing

Function 2 is executingFunction 1 is executing

.
.
.

Function 2 is executingFunction 1 is executing





注意:多线程满足您的需求,但多线程与多处理完全不同。此线程应用程序(如上例所示)一次只执行一个任务。它是你的CPU为两个线程分配时间来共同运行。我的意思是时间被划分并且每个任务都被执行。在特定时间,只执行一个线程。看起来你的程序同时执行多个任务,但事实并非如此。



NOTE: Multi-threading satisfies your need but multi-threading is entirely different from multi-processing. This threaded application(shown in the example above) performs only a single task at a time. It is your CPU which allocates time for both the threads to run together., I mean time is divided and each task is performed. At a particular time only one thread will be executed. It looks like your program is performing multiple tasks at a same time but it is not.


这篇关于在这种情况下,IF语句或多处理是否正确使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆