简体   繁体   English

如何在python中并行运行多个函数

[英]how to run multiple functions parallel in python

I want to receive serial data and depending on the data want to make announcement. 我想收到串行数据,并根据想要发布公告的数据。 my monitor function will continuously monitor serial data. 我的监控功能将持续监控串行数据。 But I am facing a problem that when i am announcing something then after completion of announcement serial data is monitored and the process going slow. 但我面临的一个问题是,当我宣布某些内容之后,在完成公告后,将监控串行数据并且流程进展缓慢。 I want to monitor serial data continuously and want to made the announcement parallel. 我想连续监视串行数据,并希望将声明并行。 Is threading is the best option?? 线程是最好的选择吗? how to handle it? 怎么办呢?

def announce(data):
   subprocess.call('espeak',data)

while 1:

    receiveddata= xbee.readline()  
    if receiveddata=='a1':
        announce("i am ok in room1")
    if receiveddata=='b2':
        announce("Urgently attend room 1")
from threading import Thread

def announce(data):
    subprocess.call('espeak',data)

class worker(Thread):
    def __init__(self, data):
        Thread.__init__(self)
        self.data = data

    def run(self):
        if receiveddata=='a1':
            announce("i am ok in room1")
        if receiveddata=='b2':
            announce("Urgently attend room 1")
        # at the end of run() the process will die.

while 1:
    receiveddata = xbee.readline()
    thread_handle = worker(receiveddata)
    thread_handle.start() # <- This starts the thread but keeps on going

Here's a skeleton framework that you can use in order to achieve paralell processing in Python. 这是一个骨架框架,您可以使用它来实现Python中的并行处理。 It's not complete nor is it perfect, but it will give you a start and it will solve your initial problem. 它不完整也不完美,但它会给你一个开始,它将解决你的初始问题。

There's tons of "best practices" for threading and stuff, i'll leave google to explain and find those because there will always be a better solution than what i can produce in one short answer here. 线程和东西有很多“最佳实践”,我会离开谷歌解释并找到那些,因为总会有一个比我在一个简短的答案中产生的更好的解决方案。

Good to know: 很高兴知道:

I honored the fact that you were new to threading in python. 我很荣幸你是python线程新手的事实。

But as discussed in the comments below, this will be a resource demanding solution if you have aa lot of data on the serial port (which will create thread -> do work -> die ). 但正如下面的评论中所讨论的,如果你在串口上有很多数据(这将create thread - > do work - > die ),这将是一个资源要求很高的解决方案。

There are more effective threading solutions (such as keeping a thread alive throughout the program and calling a function in the class that announces instead). 有更有效的线程解决方案(例如在整个程序中保持线程活动并在类中调用一个函数来代替)。 But this is the bare minimum that you'll need in order to get started. 但这是您开始使用所需的最低限度。

Once you're comfortable with threads 一旦你对线程感到满意

I'll leave these links here for you to experiment and evolve your threading knowledge from this very basic example above: 我将在这里留下这些链接,以便您从上面这个非常基本的示例中试验和发展您的线程知识:

Working with queues 使用队列

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

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