在这种情况下,IF语句或多处理是否正确使用?

编程入门 行业动态 更新时间:2024-10-11 11:24:53
本文介绍了在这种情况下,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"

。我知道你愿意在你的应用程序中实现线程。 这里是你如何实现两个函数的线程,并将两个函数一起运行。[例子] 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语句或多处理是否正确使用?

本文发布于:2023-11-12 13:12:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581598.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在这种情况下   语句   是否正确   或多

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!