使用python中的线程运行无限循环 [英] Running infinite loops using threads in python

查看:1119
本文介绍了使用python中的线程运行无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序是按以下方式设计的:

My program is designed in the following way:

  1. 程序的第一部分从传感器获取实时值,并使用 Matplotlib进行绘制.这需要长时间进行.而且,它将信息记录到数据库中.
  2. 第二部分是IP摄像机.我必须从IP摄像机获取输入并显示它.为了显示,我使用的是OpenCV的imshow方法.另外,我存储的是来自IP摄像机的视频.
  1. First part of the program takes real time values from a sensor and plots it using Matplotlib. This has to be done for long durations. And also, it logs information into a database.
  2. The second part is the IP Camera. I have to get the input from an IP Camera and display it. For displaying I am using OpenCV's imshow method. Also, I am storing the video from the IP Camera.

问题:我已经准备好算法,问题是我需要在while循环中同时运行这两个算法.条件是我不能退出任何一个.现在,线程化是一个很好的选择,但是我已经阅读了有关GIL的文章,那么我该如何运行两个无限循环呢?

Question: I have the algorithms in place, the problem is I need to run both these in a while loops. The condition is that I cannot exit from any of them. Now threading is a good alternative for this but I have read about the GIL, so how do I go about running two infinite loops?

from multiprocessing import Process

def methodA():
    while TRUE:
        do something

def methodB():
    while TRUE:
        do something

p=Process(target=methodA())
p.start()
p1=Process(target=methodB())
p1.start()

现在,当我启动进程p时,它开始执行,之后如何启动p1同时运行?

Now when I start process p it starts executing, after that how do I start p1 to run simultaneously?

推荐答案

据我所知,您有两个不同的任务要让他们连续执行.现在关于您的问题:

As far as I understood your question, you have two different tasks that you want them to perform continuously. Now regarding your questions:

如何运行两个无限循环?

您可以创建两个不同的线程来为您运行这些无限循环.第一个线程将执行您的task1,第二个线程将执行task2.

You can create two different threads that will run these infinite loops for you. The first thread will perform your task1 and second one will perform task2.

此外,一旦我开始执行一个线程,我该如何执行另一个 线程在第一个线程连续/无限运行时是什么?

Also, once I start executing a thread, how do I execute the other thread when the first thread is running continuously/infinitely?

如果您使用两个不同的线程,则无需担心此问题.如果线程不共享任何资源,那么您不必担心这个事实. 但是,如果要停止/暂停另一个线程中的一个线程,反之亦然,则可以使用标志或锁来实现一种机制.在这种情况下,这些问题会有所帮助:

If you are using two different threads then you don't need to be worried about this issue. If the threads are not sharing any resource then you don't need to worry about this fact. How ever if you want to stop/pause one thread from the other thread or vice versa then you can implement a mechanism using flags or locks. These questions will help in this case:

有没有办法杀死线程Python?

为什么python线程.Thread对象具有开始"但没有停止"吗?

制作a-program-munltithreaded

使用线程的示例示例:

from threading import Thread

class myClassA(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.daemon = True
        self.start()
    def run(self):
        while True:
            print 'A'

class myClassB(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.daemon = True
        self.start()
    def run(self):
        while True:
            print 'B'


myClassA()
myClassB()
while True:
    pass

要共享资源吗?

为其使用.这里有些例子. 一个两个

Use Locks for them. Here are some examples. One, two and How to synchronize threads in python?

如果我不想使用类运行该怎么办?我该如何仅使用方法?

from threading import Thread

def runA():
    while True:
        print 'A\n'

def runB():
    while True:
        print 'B\n'

if __name__ == "__main__":
    t1 = Thread(target = runA)
    t2 = Thread(target = runB)
    t1.setDaemon(True)
    t2.setDaemon(True)
    t1.start()
    t2.start()
    while True:
        pass

这篇关于使用python中的线程运行无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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