在python中使用线程运行无限循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23100704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running infinite loops using threads in python
提问by Prakhar Mohan Srivastava
My program is designed in the following way:
我的程序是按以下方式设计的:
- 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.
- 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.
- 程序的第一部分从传感器获取实时值并使用Matplotlib绘制它。这必须持续很长时间。而且,它将信息记录到数据库中。
- 第二部分是网络摄像机。我必须从 IP 摄像机获取输入并显示它。为了显示,我使用 OpenCV 的
imshow
方法。另外,我正在存储来自 IP 摄像机的视频。
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?
问题:我有算法,问题是我需要在 while 循环中运行这两个。条件是我不能退出任何一个。现在线程是一个很好的替代方案,但我已经阅读了 GIL,那么我该如何运行两个无限循环呢?
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?
现在当我启动进程时p
它开始执行,之后我如何开始p1
同时运行?
采纳答案by ρss
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.
您可以创建两个不同的线程来为您运行这些无限循环。第一个线程将执行您的任务 1,第二个线程将执行任务 2。
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'?
为什么 python threading.Thread 对象有“开始”,但没有“停止”?
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 Locksfor them. Here are some examples. One, twoand How to synchronize threads in python?
为他们使用锁。这里有些例子。一、二以及如何在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\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
回答by CLESIO MAXUEL BARBOZA DE SOUZA
from threading import Thread
import time
class PrintA(Thread):
def __init__(self):
Thread.__init__(self)
self.running = True
def run(self):
while self.running:
print('A')
time.sleep(1)
def stop(self):
self.running = False
class PrintB(Thread):
def __init__(self):
Thread.__init__(self)
self.running = True
def run(self):
while self.running:
print('B')
time.sleep(2)
def stop(self):
self.running = False
a = PrintA()
b = PrintB()
a.start()
b.start()
time.sleep(10)
a.stop()
time.sleep(10)
b.stop()