Skip to content

Commit

Permalink
Added client/server from #491 to demos
Browse files Browse the repository at this point in the history
  • Loading branch information
comrumino committed May 19, 2022
1 parent c6df92d commit 7bfa7ee
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
33 changes: 33 additions & 0 deletions demos/async_client/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python
"""Shows expected behavior for a client when the remote thread serving this client is busy/sleeping.
Additional context: https://github.com/tomerfiliba-org/rpyc/issues/491#issuecomment-1131843406
"""
import rpyc
import threading
import time


def async_example(connection):
t0 = time.time()
print(f"Running async example...")
_async_function = rpyc.async_(connection.root.function)
res = _async_function(threading.Event())
print(f"Created async result after {time.time()-t0}s")
value = res.value
print(f"Value returned after {time.time()-t0}s: {value}")
print()


def synchronous_example(connection):
t0 = time.time()
print(f"Running synchronous example...")
value = connection.root.function(threading.Event())
print(f"Value returned after {time.time()-t0}s: {value}")
print()


if __name__ == "__main__":
connection = rpyc.connect("localhost", 18812, config=dict(allow_public_attrs=True))
async_example(connection)
synchronous_example(connection)
20 changes: 20 additions & 0 deletions demos/async_client/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env python
"""Emulates a service function that is blocked due to being busy/sleeping.
Additional context: https://github.com/tomerfiliba-org/rpyc/issues/491#issuecomment-1131843406
"""
import rpyc
import threading
import time


class Service(rpyc.Service):
def exposed_function(self, event):
threading.Thread(target=event.wait).start()
time.sleep(1)
threading.Thread(target=event.set).start()
return 'silly sleeps on server threads'


if __name__ == "__main__":
rpyc.ThreadedServer(Service(), hostname="localhost", port=18812).start()

0 comments on commit 7bfa7ee

Please sign in to comment.