Skip to content

Instantly share code, notes, and snippets.

@stas-sl
Created August 20, 2015 11:18
Show Gist options
  • Save stas-sl/a665a9f26f8664de9294 to your computer and use it in GitHub Desktop.
Save stas-sl/a665a9f26f8664de9294 to your computer and use it in GitHub Desktop.
import Pyro4
import Pyro4.errors
import time
import threading
import signal
import multiprocessing as mp
Pyro4.config.THREADPOOL_SIZE = 100
# Pyro4.config.THREADING2 = True
# Pyro4.config.SERVERTYPE = "multiplex"
NUM = 40
class Server:
def __init__(self):
self.pings = {'MyProcess-%s' % (x+1): time.time() for x in xrange(NUM)}
def ping(self, name):
self.pings[name] = time.time()
def get_dead(self):
return sorted(k for k, v in self.pings.iteritems() if time.time() - v > 3)
class MyProcess(mp.Process):
def __init__(self):
super(MyProcess, self).__init__()
def run(self):
signal.signal(signal.SIGINT, signal.SIG_IGN)
server = Pyro4.Proxy(uris['server'])
server._pyroTimeout = 1
while 1:
try:
print self.name
server.ping(self.name)
time.sleep(1)
except Pyro4.errors.TimeoutError:
print 'reconnect %s' % self.name
server._pyroReconnect()
if __name__ == '__main__':
with Pyro4.Daemon() as daemon:
uris = {}
s = Server()
uris['server'] = daemon.register(s)
daemon_thread = threading.Thread(target=daemon.requestLoop)
daemon_thread.daemon = True
daemon_thread.start()
time.sleep(1)
for x in xrange(NUM):
p = MyProcess()
p.daemon = True
p.start()
# time.sleep(0.01)
while 1:
time.sleep(1)
print 'dead processes: %s' % s.get_dead()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment