6

I would like to use multiprocessing (to avoid GIL issues on multi core machines) and have a read process and a write process using the same serial port. Is that possible? If so, how do I get the port acquired and then how do I get the child processes file objects that they can use?

Edit -- This needs to work on Windows, but Mac and Linux would also be nice.

MPelletier
  • 16,256
  • 15
  • 86
  • 137
K. Brafford
  • 3,755
  • 2
  • 26
  • 30
  • Of course you can, at least on Unix platforms, but you'll have to use some kind of synchronization mechanism between the processes. – Fred Foo Jan 05 '12 at 16:27
  • Why would I need synchronization mechanisms? The serial port is full duplex, right? – K. Brafford Jan 05 '12 at 16:31
  • Ah, you mean one process reads and one writes? That shouldn't be a problem, I guess. (Not a serial comms expert, though.) – Fred Foo Jan 05 '12 at 16:33
  • 2
    Full duplex mean input/output communication can be done together. That's doesn't mean multiple process can use it at the same time. A serial port can be attached only one, only one client can take the port and talk at time. – tito Jan 05 '12 at 16:35
  • Yep. I want one process to write, and another process to read. I will use a queueing mechanism to interact with another process that is actually making sure that the synchronization of the actual protocol is correct. – K. Brafford Jan 05 '12 at 16:35
  • @tito, are you saying there's no way to somehow "dup" the serial port handle and use it in a child process (or child processes)? I thought a child process inherits all of the file handles of its parent? – K. Brafford Jan 05 '12 at 16:36
  • In case of fork, i think the fd will be available for both process (totally not sure about the split here). But then, you'll still have to have a lock/sync mechanism to do write at the same time, or read at the same time. That's look silly. :) – tito Jan 05 '12 at 17:36
  • Possible duplicate - http://stackoverflow.com/questions/1605721/faking-an-rs232-serial-port – ChrisF Nov 15 '15 at 21:48

1 Answers1

4

As stated in the comments, only one process can acquire the serial port at a time - therefore the way to go is to create yet another process, possibly using Python xmlrpc, or jsonrpc, that will do the actual hardware I/O, and modify your current read and write scripts to call remote functions on that other process.

The example in the library documentation should be enough for implementing such "I/O server process" with xmlrpc: http://docs.python.org/library/simplexmlrpcserver.html

Neel Basu
  • 12,638
  • 12
  • 82
  • 146
jsbueno
  • 99,910
  • 10
  • 151
  • 209