Python - global Serial obj instance accessible from multiple modules
Question: I have 5 different games written in python that run on a raspberry pi. Each
game needs to pass data in and out to a controller using a serial connection.
The games get called by some other code (written in nodeJS) that lets the user
select any of the games.

I'm thinking I don't want to open and close a serial port every time I start
and finish a game. Is there anyway to make a serial object instance "global",
open it once, and then access it from multiple game modules, all of which can
open and close at will?

I see that if I make a module which assigns a Serial object to a variable
(using PySerial) I can access that variable from any module that goes on to
import this first module, but I can see using the id() function that they are
actually different objects - different instances - when they are imported by
the various games.

Any ideas about how to do this?


Answer: Delegate the opening and management of the serial port to a separate daemon,
and use a UNIX domain socket to transfer the file descriptor for the serial
port to the client programs.

