mirror of
https://github.com/capnproto/pycapnp.git
synced 2025-03-04 16:35:04 +01:00
49 lines
1.1 KiB
Python
Executable file
49 lines
1.1 KiB
Python
Executable file
#!/usr/bin/env python
|
|
|
|
from __future__ import print_function
|
|
|
|
import argparse
|
|
import capnp
|
|
|
|
import thread_capnp
|
|
|
|
|
|
class ExampleImpl(thread_capnp.Example.Server):
|
|
|
|
"Implementation of the Example threading Cap'n Proto interface."
|
|
|
|
def subscribeStatus(self, subscriber, **kwargs):
|
|
return capnp.getTimer().after_delay(10**9) \
|
|
.then(lambda: subscriber.status(True)) \
|
|
.then(lambda _: self.subscribeStatus(subscriber))
|
|
|
|
def longRunning(self, **kwargs):
|
|
return capnp.getTimer().after_delay(3 * 10**9)
|
|
|
|
|
|
def parse_args():
|
|
parser = argparse.ArgumentParser(usage='''Runs the server bound to the\
|
|
given address/port ADDRESS may be '*' to bind to all local addresses.\
|
|
:PORT may be omitted to choose a port automatically. ''')
|
|
|
|
parser.add_argument("address", help="ADDRESS[:PORT]")
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
impl = ExampleImpl()
|
|
|
|
|
|
def restore(ref):
|
|
assert ref.as_text() == 'example'
|
|
return impl
|
|
|
|
|
|
def main():
|
|
address = parse_args().address
|
|
|
|
server = capnp.TwoPartyServer(address, restore)
|
|
server.run_forever()
|
|
|
|
if __name__ == '__main__':
|
|
main()
|