Passing params to single instance of wxApp
Sam
shendley at email.unc.edu
Wed Mar 26 19:13:14 EST 2003
> Actually, I don't think there is a better way (at least not if
> portability matters at all). But frankly, it's neither *that* ugly nor
> difficult. Take a look at the example in the Python docs for sockets to
> get started.
Thanks Cliff, I took that example and threw it straight in to my app
works great, had to cut and paste myself an Invoke Later handelr but
it works spectacuarly.
Im posting the code I used here cause I think it might be useful for
other people looking for a solution to this problem.
#########################################################
# These all need to go in your main app class
#########################################################
def startPortListener(self):
self.serverflag=Event()
athread = (Thread(target = startportlistener, args =
[self.CheckFile, self.serverflag]))
athread.setDaemon(false)
athread.start()
def onInvoke(self, event):
# print 'invoking envent'
apply(event.func, event.args, event.kwargs)
def invokeLater(self, func, args = [], kwargs = {}):
# print 'posting event'
wxPostEvent(self, InvokeEvent(func, args, kwargs))
def checkParamsLater(self, params):
# print 'laoding event'
self.invokeLater(self.CheckFile, [params])
#########################################################
# these are at root level in the file
#########################################################
wxEVT_INVOKE = wxNewEventType()
def EVT_INVOKE(win, func):
win.Connect(-1, -1, wxEVT_INVOKE, func)
class InvokeEvent(wxPyEvent):
def __init__(self, func, args, kwargs):
wxPyEvent.__init__(self)
self.SetEventType(wxEVT_INVOKE)
self.func = func
self.args = args
self.kwargs = kwargs
def main(params):
print 'params are ' + str(params)
checker = btChecker('PTC- ' + str(wxGetUserId()))
if checker.IsAnotherRunning():
if params != []:
print 'Starting Client Server'
# Echo client program
import socket
import sys
import pickle
import StringIO
HOST = socket.gethostbyname(socket.gethostname()) # The
remote host
PORT = 50007 # The same port as used by the
server
s = None
for res in socket.getaddrinfo(HOST, PORT,
socket.AF_UNSPEC, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
s = socket.socket(af, socktype, proto)
except socket.error, msg:
s = None
continue
try:
s.connect(sa)
except socket.error, msg:
s.close()
s = None
continue
break
if s is None:
print 'could not open socket'
sys.exit(1)
print 'about to send' + str(params)
pickledstring = StringIO.StringIO()
pickle.dump(params,pickledstring)
s.send(pickledstring.getvalue())
pickledstring2 = StringIO.StringIO()
data = s.recv(1024)
s.close()
print 'Received', `data`
else:
app = MyApp(params)
app.MainLoop()
def startportlistener(paramshandlerlater, doneflag):
# paramshandlerlater is a function in your main app takes the params
stores
# them and then calls your normal paramshandler from main thread
# Doneflag is an Threading.Event() in your main App, use
self.doneflag.set()
# to kill the thread the only
# problem is this thing will stay open till it gets a packet
# Echo server program
import socket
import sys
import pickle
import StringIO
HOST = '' # Symbolic name meaning the local host
PORT = 50007 # Arbitrary non-privileged port
print 'Starting Port Listener'
while not doneflag.isSet():
s = None
for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC,
socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
af, socktype, proto, canonname, sa = res
try:
s = socket.socket(af, socktype, proto)
except socket.error, msg:
s = None
continue
try:
s.bind(sa)
s.listen(1)
except socket.error, msg:
s.close()
s = None
continue
break
if s is None:
print 'could not open socket'
sys.exit(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if not data: break
pickledstring = StringIO.StringIO(data)
unpickled = pickle.load(pickledstring)
print 'unpickled is '+str(unpickled)
paramshandler(unpickled[0])
conn.send(unpickled[0])#sends back first param for
debugging
conn.close()
More information about the Python-list
mailing list