python 2.7脚本无限期并行运行两个while循环

时间:2014-01-23 21:04:02

标签: python mysql multithreading while-loop

我正在尝试在运行ubuntu的Beaglebone Black上执行一些代码。该脚本有两个主要功能: 1:计数数字脉冲 2:每隔10秒左右将计数的脉冲存储在mySQL中

这两个功能需要无限期地运行。

我的问题是如何让这两个函数并行运行?这是我最新的代码修订版本无效,因此您可以看到我尝试做的事情。任何帮助非常感谢 - 因为我是python的新手。

提前谢谢!

#!/usr/bin/python

import Adafruit_BBIO.GPIO as GPIO
import MySQLdb
import time
import thread
from threading import Thread


now = time.strftime('%Y-%m-%d %H:%M:%S')
total1 = 0
total2 = 0


def insertDB_10sec(now, total1, total2):
    while True:
        conn = MySQLdb.connect(host ="localhost", 
            user="root", 
            passwd="password", 
            db="dbname")
        cursor= conn.cursor()
        cursor.execute("INSERT INTO tablename VALUES (%s, %s, %s)",  (now, total1, total2))
        conn.commit()
        conn.close()
        print "DB Entry Made"
        time.sleep(10)

def countPulse():
    now = time.strftime('%Y-%m-%d %H:%M:%S')
    GPIO.setup("P8_12", GPIO.IN)
    total1 = 0
   total2 = 0
   while True:
    GPIO.wait_for_edge("P8_12", GPIO.RISING)
    GPIO.wait_for_edge("P8_12", GPIO.FALLING)
    now = time.strftime('%Y-%m-%d %H:%M:%S')
    print now
    total1 +=1
    print total1
    return now, total1, total2 

t1 = Thread(target = countPulse)
t2 = Thread(target = insertDB_10sec(now, total1, total2))

t1.start()
t2.start()

2 个答案:

答案 0 :(得分:2)

对于Queue来说,这是一个完美的问题!

#!/usr/bin/python

import Adafruit_BBIO.GPIO as GPIO
import MySQLdb
import time
import thread
import Queue
from threading import Thread


now = time.strftime('%Y-%m-%d %H:%M:%S')
total1 = 0
total2 = 0

pulse_objects = Queue.Queue()


def insertDB_10sec(pulse_objects):
    while True:
        now,total1,total2 = pulse_objects.get()
        conn = MySQLdb.connect(host ="localhost", 
            user="root", 
            passwd="password", 
            db="dbname")
        cursor= conn.cursor()
        cursor.execute("INSERT INTO tablename VALUES (%s, %s, %s)",  (now, total1, total2))
        conn.commit()
        conn.close()
        print "DB Entry Made"
        time.sleep(10)

def countPulse(pulse_objects):
    now = time.strftime('%Y-%m-%d %H:%M:%S')
    GPIO.setup("P8_12", GPIO.IN)
    total1 = 0
    total2 = 0
    while True:
        GPIO.wait_for_edge("P8_12", GPIO.RISING)
        GPIO.wait_for_edge("P8_12", GPIO.FALLING)
        now = time.strftime('%Y-%m-%d %H:%M:%S')
        print now
        total1 +=1
        print total1
        pulse_objects.put( (now, total1, total2) )

t1 = Thread(target = countPulse, args = (pulse_objects,))
t2 = Thread(target = insertDB_10sec, args = (pulse_objects,))

t1.start()
t2.start()

答案 1 :(得分:1)

为什么需要两个线程?将插入移动到countPulse。话虽如此:

您不应在此致电insertDB_10sec

t2 = Thread(target = insertDB_10sec(now, total1, total2))

将参数作为实际参数提供:

t2 = Thread(target = insertDB_10sec, args=(now, total1, total2))

这仍然无法达到您的意图,因为now, total1, total2countPulse中的局部变量。而是将它们声明为全局:

global now
global total1
global total2

另一个线程也是如此,因为你在开始时传递变量。全部删除insertDB_10sec的参数。

为什么您希望每十秒钟存储一个新的数据库条目,而不管其他线程是做什么的?您的评论表明now的字段是唯一的。因此,在尝试再次插入之前,应首先检查now是否已更改。或者将INSERT INTO替换为REPLACE INTO