简体   繁体   中英

python 2.7 script running two while loops in parallel indefinitely

I am trying to execute some code on a Beaglebone Black running ubuntu. The script has two primary functions: 1: count digital pulse 2: store the counted pulses in mySQL every 10s or so

These two functions need to run idefinitely.

My question is how to do get these two functions to run in parallel? Here is my latest code revision that doesn't work so you can see what I am attempting to do. Any help much appreciated -- as I new to python.

Thank you in advance!

#!/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()

This is a perfect problem for a 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()

Why do you need two threads? Move the insert to countPulse . That being said:

You should not call insertDB_10sec here:

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

Provide the arguments as actual arguments:

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

This will still not do what you intend, because now, total1, total2 are local variables in countPulse . Instead declare them as global:

global now
global total1
global total2

The same holds for the other thread, because you pass the variables there at start. Remove the arguments to insertDB_10sec all together.

Why would you want to store a new database entry every ten seconds regardless of what the other thread does? Your comment suggests that the field for now is unique. Therefore you should first check whether now changed before you try to insert again. Or replace INSERT INTO with REPLACE INTO .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM