Asynchronous Serial Port Python
This seems to be a common problem, since there is a question about how to do it a few times a month in comp.lang.pyhton. There are other solutions, involving synchronisation between threads that will allow you to handle the problem without the polling (the root.after()), but this is rather un-neat, since you tend to get raising and waiting for semaphores all over your code. In any case, a GUI already has a bunch of polling mechanisms built into it (the main event loop), so adding one won't make much difference - especially since it runs fairly seldom. The code has only been tested under Linux, but it should work on any platform with working threads. (.continued from previous comment) # Start the timer -- this replaces the initial call to periodicCall self.timer.start(100) # Set up the thread to do asynchronous I/O # More can be made if necessary self.running = 1 self.thread1 = threading.Thread(target=self.workerThread1) self.thread1.start() def periodicCall(self): '' Check every 100 ms if there is something new in the queue.
'' self.gui.processIncoming() if not self.running: root. Call Of Duty 4 Profile Level 55 Guess more. quit() def endApplication(self): self.running = 0 def workerThread1(self): '' This is where we handle the asynchronous I/O. For example, it may be a 'select()'. One important thing to remember is that the thread has to yield control. '' while self.running: # To simulate asynchronous I/O, we create a random number at # random intervals. Replace the following 2 lines with the real # thing.
Python Serial Port Extension - Asynchronous I/O support. (8 replies) I am attempting to write a program (to run on a linux box) in python which communicates to a device via a serial port. The problem is that the device is.
Time.sleep(rand.random() * 0.3) msg = rand.random() self.queue.put(msg) rand = random.Random() root = qt.QApplication(sys.argv) client = ThreadedClient() root.exec_loop() It's amazing how similar these are. Two improvements: it doesn't use sys.exit, but asks the QApplication instance to quit, and it shows the result of the thread in the gui. Here is an updated version of Boudewiyn's code, for PyQT, Qt 4.7 '' This recipe describes how to handle asynchronous I/O in an environment where you are running PyQt as the graphical user interface. PyQt is safe to use as long as all the graphics commands are handled in a single thread. Since it is more efficient to make I/O channels to block and wait for something to happen rather than poll at regular intervals, we want I/O to be handled in separate threads. These can communicate in a threasafe way with the main, GUI-oriented process through one or several queues.