私がやっているコントローラプロジェクトのためにいくつかの非同期メッセージングをしようとしていますが、次の関数の目的はコールバックをappjar app.registerEvent
コールに戻して、モータ制御UIのステータスセクション。asyncioを使ってクロージャを処理する方法
def motorStatusMonitor(loop: aio.AbstractEventLoop, app: aj.appjar.gui, Messenger: cmd.PyCmdMessenger.CmdMessenger)-> Callable:
async def getStatus(aFuture):
nonlocal Messenger
nonlocal app
# Ask for the current status of the motor
t = await Control.sendCommand(Messenger, "Status")
d = {t[1][0]: t[1][1], t[1][2]: t[1][3]} # parse the response
def statusChanger(): # need a closure to write to the app when called
nonlocal d # use the message from above
nonlocal app # use the app passed with motorStatusMonitor
app.openTab("Main", "Control")
app.openLabelFrame("Status")
app.setLabel("motorStatus", f"Motor A: \t\t {get('A', d, '???')}\nMotor B: \t\t {get('B', d, '???')}") # Print the status of the motors to the app
aFuture.set_result(statusChanger)
future = aio.Future()
aio.ensure_future(getStatus(future))
loop.run_until_complete(future)
return future.result()
ただし、これは機能しません。私が行ったときのように、それは永遠にハングします。
は、どのように私は実際にここに非同期の実装について行くべきか?すべてのための
完全なコードはGithubです。
あなたが行っていることを示す実際のコードがない場合、他の人がどのように助けてくれると思いますか? – Ding
質問をする前に誤って提出しました。私の間違いでした。 – nick5435
実際に何を達成しようとしていますか?閉鎖は問題ではないので。少なくとも、クロージャを使用せず、代わりにグローバルな 'app'を使用するようにコードを修正することができます。私は閉鎖が適切なアプローチではないと言っているわけではありません(たとえそれがPythonであまり一般的でない場合でも)私はあなたがXYの問題を嫌うかもしれないと言っていますhttps://meta.stackexchange.com/questions/66377/what-is -the-xy-problem。あなたが達成しようとしているものを拡大してください。 – amirouche