[英]Python: Asyncio NATS.io blocking
I have troubles to make Python Asyncio NATS.io running sequentialy. 我很难使Python Asyncio NATS.io顺序运行。 I have two classes:
Account
and Bridge
Account holds the logic of application and it is communicating thought Bridge
with external service via NATS.io. 我有两个类:
Account
和Bridge
账户持有应用程序的逻辑,它是沟通思想Bridge
通过NATS.io.外部服务
Main
file: Main
文件:
loop = asyncio.get_event_loop()
account = Account(loop, options)
asyncio.async(account.start())
loop.run_forever()
Account
class: Account
类别:
class Account:
bridge = Bridge()
def connect(self):
result = self.bridge.connect(self.id)
return result
Bridge
class: Bridge
类:
def connect(self, account_id):
data = None
try:
response = yield from self.nc.timed_request("bank.account.connect",
BankRequest(
method="connect",
data={...}
), 10)
data = json.loads(response.data.decode())
except ErrTimeout:
status = Messages.REQUEST_TIMED_OUT
return Result(data=data)
I need to call account.connect()
from anywhere inside account class and get result of connection (sequentialy). 我需要从帐户类内部的任何位置调用
account.connect()
并获取连接结果(顺序)。 now I'm getting generator
object 现在我得到
generator
对象
your connect()
methods should probably be coroutines: 您的
connect()
方法可能应该是协程:
class Account:
bridge = Bridge() # you probably want to put this in `def __init__(self)`!
@asyncio.coroutine
def connect(self):
result = yield from self.bridge.connect(self.id)
return result
class Bridge:
@asyncio.coroutine
def connect(self, account_id):
data = None
try:
response = yield from self.nc.timed_request("bank.account.connect",
BankRequest(
method="connect",
data={...}
), 10)
data = json.loads(response.data.decode())
except ErrTimeout:
status = Messages.REQUEST_TIMED_OUT
return Result(data=data)
and: 和:
resp = yield from account.connect()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.