Socket connection open all the time

Is there a way to have a script open a socket and reference the socket in other scripts?

For example, can I create a java socket connection on Gateway startup? Reference the socket connection in a timer script or data change script?

The application involves reading and writing data out of a several socket connections continuously. The data from the socket will be parsed and stored in a database. I’ve been able to read and write data from a socket in a script but I can figure out how to use the open socket again the next time the script runs. In other words, when the script is done running the object is dead. Which means I’d have to open and close a socket connection every time the script runs. This would be very inefficient.

Any advice would be appreciated.

Sure this can be done. You can store your socket object in a Python project module attribute.
First you need to create your module variable in a project module library script.
After that you can store your socket in it and retrieve it in other scripts.

Example of storing and retrieving in a script outside the project.mymodule module:

if project.mymodule.socket == None:
	#code to make a socket
	#....
	#store your socket
	project.mymodule.socket = socket
else:
	#get your socket
	socket = project.mymodule.socket

The key is using module level variables to hold data so can you use it anywhere in the running program.

Best,