I store all futures in a list, and then pick up ready objects, read results. How to achieve this? Thank you in advance.
future_list = map(n.rpc.some_service.func.call_async, range(10))
while len(future_list) > 0:
for i in future_list:
if i.ready(): # if i.result(timeout=10ms)
print(i.result())
# pop ready objects from future_list
The .ready()
method does not accept a timeout
argument. You instead need to define the timeout on the proxy, and then .ready()
will raise an RpcTimeout
if the result is not returned in that time.
Unfortunately the helper proxy in the shell in created without a timeout argument, so you’ll have to construct your own. Like this:
# client
from nameko.standalone.rpc import ServiceRpcProxy
rpc_proxy = ServiceRpcProxy("service", {"AMQP_URI":"amqp://guest:guest@localhost:5672/"}, timeout=5)
results = []
with rpc_proxy as proxy:
for i in range(10):
results.append(proxy.sleep.call_async(i))
for res in results:
try:
print(res.result())
except Exception as e:
print(e)
# server
import time
from nameko.rpc import rpc
class Service:
name = "service"
@rpc
def sleep(self, arg):
time.sleep(arg)
return arg
Unfortunately, there’s also a problem with the timeout implementation in the standalone proxy. If you make multiple calls at the same time and any of them return successfully, it resets the timeout, so your requests may live for longer than you expect. In this example all of the calls succeed, even though you’d expect the last 5 to fail.
Thanks for your reply. I will read code of standalone proxy to figure out why last 5 requests did not fail.