#Main.py : ################### import webapp2 from threading import Thread import time from Queue import Queue from control_service import ControlService import machine from message import Message class MainPage(webapp2.RequestHandler): def get(self): #each queue is one direction q_CL_CS = Queue() #client --> control service, # q_CS_CL = Queue() #not yet q_CS_M = Queue() #control service --> machine q_M_CS = Queue() #machine --> control service controlService = ControlService(q_CL_CS,q_CS_M,q_M_CS) controlService.startDaemon() m = Thread(target=machine.machine_d, args=(q_CS_M,q_M_CS,)) m.setDaemon(True) m.start() #simulate request from client in 6 seconds.. values have no meaning time.sleep(6) msg = Message(q_CL_CS,'req','info',1) msg.sendMessage() msg.printMessage() #let threads communicate.. time.sleep(10) self.response.headers['Content-Type'] = 'text/plain' self.response.write('Hello, World! 3') print "end" application = webapp2.WSGIApplication([('/', MainPage),], debug=True) #control_service.py : ##################### import time from message import Message from threading import Thread class ControlService(object): def __init__(self,q_CL_CS,q_CS_M,q_M_CS): self.state = -1 # self.q_CS_CL = q_CL_CS self.q_CL_CS = q_CL_CS self.q_CS_M = q_CS_M self.q_M_CS = q_M_CS # self.thread = None def setState(self,state): self.state = state def getState(self): return self.state def daemonFunc(self,q_CL_CS,q_CS_M,q_M_CS): while True: #wait for a request from client msg = q_CL_CS.get(block=True) msg.printMessage() #not yet - process request if needed here.. msg = Message(q_CS_M,'req','info',2) msg.sendMessage() msg.printMessage() msg = q_M_CS.get(block=True) # get request from machine #if response was no error then self.setState(msg.value) msg.printMessage() #not yet - send response to client time.sleep(1) def startDaemon(self): cs = Thread(target=self.daemonFunc, args=(self.q_CL_CS,self.q_CS_M,self.q_M_CS,)) cs.setDaemon(True) cs.start() #machine.py : ############# import time from message import Message def machine_d(q_CS_M,q_M_CS):#q2 while True: msg = q_CS_M.get(block=True) msg.printMessage() msg = Message(q_M_CS,'res','info',3) msg.sendMessage() msg.printMessage() time.sleep(1) #message.py : ############# class Message(object): def __init__(self,queue,direction,t,val): self.direction = direction self.queue = queue self.type = t self.value = val # self.time = None #fill when sending def sendMessage(self): self.queue.put(self) def printMessage(self): print ("Queue: " ,self.queue,"direction: ",self.direction,"type: ",self.type, "val: ", self.value)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question