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

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

问题描述

我的程序是这样设计的:

  1. 程序的第一部分从传感器获取实时值,并使用 Matplotlib 绘制它. 这必须持续很长时间.此外,它还会将信息记录到数据库中.
  2. 第二部分是网络摄像机.我必须从 IP 摄像机获取输入并显示它.为了显示,我使用 OpenCV 的 imshow 方法.此外,我正在存储来自 IP 摄像机的视频.

问题:我有算法,问题是我需要在 while 循环中运行这两个.条件是我不能退出任何一个.现在线程是一个很好的替代方案,但我已经阅读了 GIL,那么我该如何运行两个无限循环?

from multiprocessing import Process定义方法A():为真时:做点什么定义方法B():为真时:做点什么p=过程(目标=methodA())p.start()p1=进程(目标=methodB())p1.start()

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

解决方案

据我了解您的问题,您有两个不同的任务需要它们连续执行.现在关于您的问题:

<块引用>

如何运行两个无限循环?

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

<块引用>

另外,一旦我开始执行一个线程,我如何执行另一个当第一个线程连续/无限运行时线程?

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

有没有办法杀死一个线程蟒蛇?

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

制作-a-program-munltithreaded

使用线程的示例:

from threading import Thread类 myClassA(线程):def __init__(self):Thread.__init__(self)self.daemon = 真self.start()定义运行(自我):而真:打印A"类 myClassB(线程):def __init__(self):Thread.__init__(self)self.daemon = 真self.start()定义运行(自我):而真:打印'B'我的类A()我的类B()而真:经过

<块引用>

对于共享资源?

为它们使用.这里有些例子.一个两个如何在python中同步线程?

<块引用>

如果我不想使用类运行它怎么办?我如何只使用方法来做到这一点?

from threading import Threaddef runA():而真:打印 'A
'def runB():而真:打印 'B
'如果 __name__ == "__main__":t1 = 线程(目标 = runA)t2 = 线程(目标 = runB)t1.setDaemon(真)t2.setDaemon(真)t1.开始()t2.start()而真:经过

My program is designed in the following way:

  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.

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()

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:

how do I go about running two infinite loops?

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:

Is there any way to kill a Thread in Python?

Why does the python threading.Thread object has 'start', but not 'stop'?

making-a-program-munltithreaded

Sample example using threading:

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

For shared resources?

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

what if I don't want to run it using classes? How do I do this using only methods?

from threading import Thread

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

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

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天全站免登陆