当前位置: 动力学知识库 > 问答 > 编程问答 >

multithreading - Python global variable thread not updating

问题描述:

Global variable not updating in one thread.

After the serial message is received the API should exit the while loop and respond but global flag stays at 0:

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

When I poke it with "t" via the api which should elicit a responce I get:

 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

UPDATE: edited code for os.pid

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

Update:

tried using queue to communicate across threads no luck

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

output:

Port open

http://0.0.0.0:9091/

Recived: t

Waiting0

Serial < 1100D00H00M00S

Recived: t

Waiting0

Serial < 1100D00H00M00S

Recived: t

Waiting0

Serial < 1100D00H00M00S

Recived: t

Waiting0

Serial < 1100D00H00M00S

Recived: t

Waiting0

Serial < 1100D00H00M00S

Recived: t

分享给朋友:
您可能感兴趣的文章:
随机阅读: