全局变量不在一个线程中更新。
收到串行消息后,API应退出while循环并响应,但全局标志保持为0:
import sqlite3, time, datetime, random, random
class DatabaseManager(object):
def __init__(self, db):
self.conn = sqlite3.connect(db)
self.conn.execute('pragma foreign_keys = on')
self.conn.commit()
self.c = self.conn.cursor()
def query(self, arg):
self.c.execute(arg)
self.conn.commit()
return self.c
def fetch(self):
self.c.fetchall()
self.conn.commit()
return self.c
def __del__(self):
self.conn.close()
dbmgr = DatabaseManager("connect.db")
while 1 > 0:
def quantity():
x = 1
file = open("john.txt", "r")
for line in file:
x = random.randint(100,10000000)
func = "INSERT INTO test (playerNAME) VALUES (?, ?)", line, x
dbmgr.query(func)
当我通过api用“t”戳它时,我会得到一个响应:
#! /usr/bin/env python
from threading import Thread
import serial
import time
import web
import os
receivedFlag = 0;
last_received = ''
port = serial.Serial("/dev/ttyUSB1", baudrate=9600, timeout=1.0)
urls = (
'/', 'index'
)
def receiving(ser):
global last_received
global last_toSend
global receivedFlag
buffer_string = ''
while True:
buffer_string = buffer_string + ser.read(ser.inWaiting())
if '\n' in buffer_string:
lines = buffer_string.split('\n')
last_received = lines[-2]
buffer_string = lines[-1]
print "Serial < "+ last_received
print "serial thread os.pid: " + str(os.getpid())
receivedFlag = 1;
class index:
def GET(self):
global last_toSend
global port
global receivedFlag
data = web.input()
print "Recived: " + data.line
port.write(data.line)
receivedFlag = 0;
while receivedFlag == 0:
time.sleep(0.2)
print "api thread os.pid: " + str(os.getpid()) +" Waiting for " + str(receivedFlag)
web.header('Content-Type', 'jsonp')
print "Responding: " + last_received
return "localJsonpCallback({\"box\": \"" + last_received + "\"})"
if __name__ == "__main__":
time.sleep(1)
print("Port open")
Thread(target=receiving, args=(port,)).start()
app = web.application(urls, globals())
app.run()
更新:已编辑的os.pid代码
Port open
http://0.0.0.0:9091/
Recived: t
Waiting for0
Waiting for0
Waiting for0
Waiting for0
Waiting for0
Serial < 1100D01H31M08S
Waiting for0
Waiting for0
Waiting for0
Waiting for0
Waiting for0
Waiting for0
....
and on and on
更新: 尝试使用队列跨线程进行通信没有运气
Port open
http://0.0.0.0:9091/
Recived: t
api thread os.pid: 7942 Waiting for 0
api thread os.pid: 7942 Waiting for 0
api thread os.pid: 7942 Waiting for 0
api thread os.pid: 7942 Waiting for 0
api thread os.pid: 7942 Waiting for 0
Serial < 1100D00H00M00S
serial thread os.pid: 7942
api thread os.pid: 7942 Waiting for 0
api thread os.pid: 7942 Waiting for 0
api thread os.pid: 7942 Waiting for 0
输出:
#! /usr/bin/env python
from threading import Thread
import serial
import time
import web
import os
from Queue import Queue
qb = Queue()
port = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=1.0)
urls = (
'/', 'index'
)
def receiving(ser):
global qb
buffer_string = ''
while True:
buffer_string = buffer_string + ser.read(ser.inWaiting())
if '\n' in buffer_string:
lines = buffer_string.split('\n')
line = lines[-2]
qb.put(line)
buffer_string = lines[-1]
print "Serial < "+ line
class index:
def GET(self):
global port
global qb
data = web.input()
print "Recived: " + data.line
port.write(data.line)
got = None
while got == None:
time.sleep(1)
print "Waiting" + str(qb.qsize())
got = qb.get()
qb.task_done()
print "exitloop"
web.header('Content-Type', 'jsonp')
print "Responding: " + str(got)
return "localJsonpCallback({\"box\": \"" + str(got) + "\"})"
if __name__ == "__main__":
time.sleep(1)
print("Port open")
t1 = Thread(target=receiving, args=(port,))
t1.start()
app = web.application(urls, globals())
app.run()
t1.join()