3
これは私の現在のコードです:この例ではasyncioを使ってn秒間に関数を再スケジュールするためのpythonデコレータ関数を作るにはどうすればよいですか?
import asyncio
import serial
def repeat(seconds):
def wrap(func):
def decorated(*args):
loop = asyncio.get_event_loop()
loop.call_at(loop.time() + seconds, decorated, *args)
print('scheduled')
func(*args)
return func
return decorated
return wrap
@repeat(10)
def send_command(ser, text):
try:
if text == 'DATA':
print("\nSending Data Request")
ser.write(b'\n022022')
elif text == 'STAY':
print("\nInmediate Stay ARM")
ser.write(b'\n0640010002044D')
elif text == 'AWAY':
print("\nInmediate Away ARM")
ser.write(b'\n0640010003044E')
elif text == 'DISARM':
print("\nDISARM")
ser.write(b'\n0940010001040700055B')
elif text == 'CHIME':
print("\nChime toggle")
ser.write(b'\n0640010007014F')
except serial.SerialException as e:
raise e
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyUSB0', 9600, parity=serial.PARITY_ODD, write_timeout=0, timeout=0)
loop = asyncio.get_event_loop()
loop.call_at(loop.time() + 5, send_command, ser, 'DATA')
loop.run_forever()
私はその後5秒と10秒ごとに実行するようにsend_command機能をスケジュールしようとしています。それは動作するようですが、私はちょっとloop.call_at(loop.time() + seconds, decorated, *args)
コールと混同しています。コメントは感謝します。
がloop.call_at' 'についてコメントはない、あなたのコードは、戻りfunc''で働いていないようです'func(* args)'でなければなりません –
'loop.call_at'のどの部分があなたを混乱させるのでしょうか? [doc](https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.AbstractEventLoop.call_at)によれば、指定された場所で呼び出されるコールバック( 'decorated')をスケジュールするだけです(loop.time()+ seconds') –
私は実際の関数呼び出しfunc(* args)を含んでいませんでした。既に編集済みです。私は内側の機能(装飾)の呼び出しと混乱して、将来の予定に変更します。 –