Source code for distlock2.service

"""
Module **sesf.distlock2.service** has class ``Service``, which is 
the concrete version of distlock2 service. RPC-accessable via pyrodaemon 
`sesf.distlock2.service`.  Run ``python service.py`` to start
a Service instance, which can then be accessed by a distributed program
that uses distlock2 service.
"""

from sesf.distlock2.serviceparts import ServiceParts
import Pyro4, argparse, time, random
from threading import Thread, Lock, Condition
from sesf.util.service import (do_service_output, do_service_input)
from sesf.util.pyro import (start_pyrodaemon)


def random_delay():
  time.sleep(random.random()*1)

# Expose class instance to Pyro4 RPCs
[docs]@Pyro4.expose class Service(): def __init__(self): self.serviceparts = ServiceParts() self.lck = Lock() self.cond = Condition(self.lck) # start Pyro4 daemon, register with Pyro4 nameserver, enter requestLoop Thread(name='pyrodaemon', target=start_pyrodaemon, args=(self, 'sesf.distlock2.service')).start() def acq(self, j): x = self.serviceparts random_delay() # print(j, "x.acq call", flush=True) # import pdb; pdb.set_trace() do_service_input(j, x.acq_CIC, x.acq_CU, (None,), self.cond) rval = do_service_output(j, x.acq_ROC, x.acq_RU, self.cond) # rval == () # rval = rval[0] random_delay() # print(j, "x.send return", rval, flush=True) # return rval def rel(self, j): x = self.serviceparts random_delay() # print(j, "x.rel call", flush=True) do_service_input(j, x.rel_CIC, x.rel_CU, (None,), self.cond) rval = do_service_output(j, x.rel_ROC, x.rel_RU, self.cond) # rval == () # rval = rval[0] random_delay() # print(j, "x.recv return", rval, flush=True) # return rval @Pyro4.oneway def end(self, j): x = self.serviceparts random_delay() # print(j, "x.end call", flush=True) do_service_input(j, x.end_CIC, x.end_CU, (None,), self.cond) rval = do_service_output(j, x.end_ROC, x.end_RU, self.cond) time.sleep(2) self.pyrodaemon.shutdown() return rval
###### End ServiceModel ######################################### if __name__ == '__main__': print('starting distlock2.service', flush=True) service = Service()