python多线程执行一个循环_使用python中的线程运行无限循环

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.

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:

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?

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值