私は、以前のコマンドの処理が終了していなくても反応し続けるスラックでボットを作ろうとしていたので、ロックせずに何か時間がかかることがありました。それは最初に終わったものを返すべきです。Pythonスラックボットを非同期にする
私はそこに途中で入っていると思います。以前のコマンドが実行を終了する前に入力されたものを無視しないようになりました。しかし、それでもスレッドはお互いを「追い越す」ことはできません。完了までに時間がかかっても、最初に呼び出されたコマンドが最初に返されます。
import asyncio
from slackclient import SlackClient
import time, datetime as dt
token = "my token"
sc = SlackClient(token)
@asyncio.coroutine
def sayHello(waitPeriod = 5):
yield from asyncio.sleep(waitPeriod)
msg = 'Hello! I waited {} seconds.'.format(waitPeriod)
return msg
@asyncio.coroutine
def listen():
yield from asyncio.sleep(1)
x = sc.rtm_connect()
info = sc.rtm_read()
if len(info) == 1:
if r'/hello' in info[0]['text']:
print(info)
try:
waitPeriod = int(info[0]['text'][6:])
except:
print('Can not read a time period. Using 5 seconds.')
waitPeriod = 5
msg = yield from sayHello(waitPeriod = waitPeriod)
print(msg)
chan = info[0]['channel']
sc.rtm_send_message(chan, msg)
asyncio.async(listen())
def main():
print('here we go')
loop = asyncio.get_event_loop()
asyncio.async(listen())
loop.run_forever()
if __name__ == '__main__':
main()
私はスラックチャットウィンドウに/hello 12
と/hello 2
を入力
/hello 12
コマンドの実行が完了するまでは、
/hello 2
コマンドは処理されません。 asyncioの理解は進んでいるので、私は非常に基本的なエラーを作り出している可能性があります。私は
previous questionで、
sc.rtm_read()
のようなものが機能をブロックしていると言われました。それは私の問題の根源ですか?
おかげで多くは、何が起こっている アレックス
どのOSとどのバージョンのPython(2または3)ですか? –
3.5、asyncioはPython 3.3以降でしか利用できないと思います。 –