jvalenzuela wrote:
Are you saying different scripts share the same scope/namespace? How would I access a class defined in a different script?
I'm not quite sure if that is appropriate for handling a socket object. I only want one object, the socket itself. I want it initialized once when the project starts, and simply call its methods in another script.
Here is a code sample:
In your init script:
global G
G = {}
G['is_ready'] = False
message.string_set("Initializing...")
import socket
class MySocket(socket.socket):
"""define your socket here"""
pass
G['socket'] = MySocket
G['is_ready'] = True
In another script do:
global G
while True:
message.string_set("Waiting ready state...")
event.wait(1000)
try:
if G['is_ready']:
break
else:
message.string_set("Waiting ready state")
except:
message.string_set("Exception: Waiting ready state...")
message.string_set("STARTED.")
# instantiate the socket
mysocket = G['socket']()
Note: you will need to subclass the socket if you want to use it in different scripts and implement some locking. That's the tricky part.
you can also use the state variable rather than a global G.
If you want to use the socket in only one script, why do you want to initialize it in another script? Can't you just use a threaded script like this?
import socket
def init_socket():
# initialize socket...
s = socket.socket(....
...
return s
s = init_socket()
while True:
try:
# use socket
except Exception:
event.wait(100)
s = init_socket()