If you want to go async, and you're comfortable with Twisted, the framework supports serial port input.If like Roman suggests blocking reads meet your needs, maybe you don't need a thread - the entire application just blocks. If you do need to block in a different thread, communicate between threads using Python's thread-safe Queue: http://docs.python.org/library/queue.html
On Thu, Oct 1, 2009 at 9:25 PM, Roman Yakovenko <[email protected]>wrote: > On Thu, Oct 1, 2009 at 9:10 PM, Omer Zak <[email protected]> wrote: > > I am developing a Python application, which reads bytes from a serial > > port (using pySerial). > > Those bytes combine into packets by means of a non-trivial set of rules. > > > > Using a chain of generators, I implemented the rules in a > > straightforward way. A generator reads bytes from the serial port, > > processes escape sequences and yields the resulting characters to > > another generator. The next generator delimits packets and yields each > > packet as a unicode string. The final generator parses the unicode > > strings and yields the parse results for each frame as a data structure > > (a Dict, whose elements are the various packet fields). > > > > However, the ultimate input of the generators needs to be from the > > serial port. > > > > The question: do I have to use a separate thread for reading bytes from > > the serial port (by means of the first generator in chain) and then use > > some complicated mechanism for transferring the yielded data structures > > to another thread? > > It depends. If you can effort single-thread & busy-wait - just do it > this way. It is easy to understand and support. If you don't, than > welcome to a wonderful world of asynchronous IO. In this case I > suggest you to read http://www.kegel.com/c10k.html article. Also it > talks about web servers, but the solutions are applicable to your > problem too. > > > The reads in such a case would be blocking reads. > > > > Or is there another idiom for feeding data for processing by a chain of > > generators, which can relieve me from having to use a separate thread? > > I am not sure whether you can completely avoid threads, but a good > async. package/library can simplify a lot of things. Boost.Asio ( C++ > ) does it very well. > > HTH > > -- > Roman Yakovenko > C++ Python language binding > http://www.language-binding.net/ > _______________________________________________ > Python-il mailing list > [email protected] > http://hamakor.org.il/cgi-bin/mailman/listinfo/python-il > -- Check out my blog: http://orip.org
_______________________________________________ Python-il mailing list [email protected] http://hamakor.org.il/cgi-bin/mailman/listinfo/python-il
