Skip to content

Instantly share code, notes, and snippets.

@lov3catch
Last active August 29, 2015 14:26
Show Gist options
  • Save lov3catch/29d75c1853920158f59d to your computer and use it in GitHub Desktop.
Save lov3catch/29d75c1853920158f59d to your computer and use it in GitHub Desktop.
Example of making controller for server (start/stop)
class Statuses(object):
ON = 1
OFF = 2
class Singleton(type):
_instance = None
def __call__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instance
class ControlMixin(object):
status = Statuses.OFF
def start(self):
print 'I`am running'
self.status = Statuses.ON
def stop(self):
print 'I`am stopping'
self.status = Statuses.OFF
def is_enable(self):
return self.status == Statuses.ON
def is_disable(self):
return self.status == Statuses.OFF
class Server(ControlMixin):
__metaclass__ = Singleton
def __init__(self, func, *args, **kwargs):
self.func = func
self.f_args = args
self.f_kwargs = kwargs
def loop_run(self):
if self.is_disable():
raise SystemError('Can`t run loop while server is not running')
print 'run processing...'
for i, x in enumerate(xrange(100)):
self.func(self.f_args, self.f_kwargs)
if self.is_disable():
break
print 'stop processing...'
def main():
def same_func(*args, **kwargs):
print 'I`m calling!'
p = Server(same_func)
p.start()
p.loop_run()
p.stop()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment