![](/img/trans.png)
[英]Is there a way to stop and restart a self hosted Python script using Github Actions?
[英]Python Pycharm Script Self-Restart
我正在制作一個 discord 機器人,我希望能夠通過命令重新啟動它。 我在客戶端事件正下方的主腳本中加載了用於錯誤、onmessage 等的命令,如下所示:
@_client.command(name='reboot')
async def reboot(command_context: commands.Context):
~some unimportant code that i have for debugging and user verification
os.execv(sys.executable, ['py']+sys.argv
通過在 discord 聊天中調用 )reboot 運行此程序后,我的機器人正在監視,我的程序以退出代碼 0 退出並且不會重新啟動。
這是我發現一些對我不起作用的地方: Restart python-script from inside itself
有些東西,不確定其中的相關性如何:
我發現的另一件事是,當嘗試使用 exit() 停止程序時,它需要一段時間然后給我這個錯誤消息(為大小道歉,不知道什么是重要的,什么不是):
Exception in thread Thread-16:
Traceback (most recent call last):
File "C:\Users\lkapp\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1016, in _bootstrap_inner
self.run()
File "C:\Users\lkapp\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1378, in run
self.function(*self.args, **self.kwargs)
File "C:\Users\lkapp\PycharmProjects\Imouto\cog_rcon.py", line 64, in <lambda>
asyncio.run_coroutine_threadsafe(
File "C:\Users\lkapp\AppData\Local\Programs\Python\Python310\lib\asyncio\tasks.py", line 885, in run_coroutine_threadsafe
loop.call_soon_threadsafe(callback)
AttributeError: '_MissingSentinel' object has no attribute 'call_soon_threadsafe'
C:\Users\lkapp\AppData\Local\Programs\Python\Python310\lib\threading.py:1018: RuntimeWarning: coroutine 'Server_Bridge.update_player_count_in_discord_activity' was never awaited
self._invoke_excepthook(self)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Task was destroyed but it is pending!
task: <Task pending name='Task-1' coro=<ARC.listenForData() running at C:\Users\lkapp\PycharmProjects\Imouto\bec_rcon.py:518>>
Task was destroyed but it is pending!
task: <Task pending name='Task-2' coro=<ARC.keepAliveLoop() running at C:\Users\lkapp\PycharmProjects\Imouto\bec_rcon.py:554>>
sys:1: RuntimeWarning: coroutine 'ARC.listenForData' was never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
C:\Users\lkapp\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py:671: RuntimeWarning: coroutine 'ARC.keepAliveLoop' was never awaited
self._ready.clear()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Task exception was never retrieved
future: <Task finished name='discord.py: on_message' coro=<Client._run_event() done, defined at C:\Users\lkapp\PycharmProjects\Imouto\venv\lib\site-packages\discord\client.py:401> exception=SystemExit()>
Traceback (most recent call last):
File "C:\Users\lkapp\PycharmProjects\Imouto\venv\lib\site-packages\discord\client.py", line 828, in run
asyncio.run(runner())
File "C:\Users\lkapp\AppData\Local\Programs\Python\Python310\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
File "C:\Users\lkapp\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 633, in run_until_complete
self.run_forever()
File "C:\Users\lkapp\AppData\Local\Programs\Python\Python310\lib\asyncio\windows_events.py", line 321, in run_forever
super().run_forever()
File "C:\Users\lkapp\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 600, in run_forever
self._run_once()
File "C:\Users\lkapp\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 1896, in _run_once
handle._run()
File "C:\Users\lkapp\AppData\Local\Programs\Python\Python310\lib\asyncio\events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "C:\Users\lkapp\PycharmProjects\Imouto\venv\lib\site-packages\discord\client.py", line 409, in _run_event
await coro(*args, **kwargs)
File "C:\Users\lkapp\PycharmProjects\Imouto\main.py", line 43, in on_message
await _client.process_commands(message)
File "C:\Users\lkapp\PycharmProjects\Imouto\venv\lib\site-packages\discord\ext\commands\bot.py", line 1389, in process_commands
await self.invoke(ctx) # type: ignore
File "C:\Users\lkapp\PycharmProjects\Imouto\venv\lib\site-packages\discord\ext\commands\bot.py", line 1347, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\lkapp\PycharmProjects\Imouto\venv\lib\site-packages\discord\ext\commands\core.py", line 986, in invoke
await injected(*ctx.args, **ctx.kwargs) # type: ignore
File "C:\Users\lkapp\PycharmProjects\Imouto\venv\lib\site-packages\discord\ext\commands\core.py", line 190, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\lkapp\PycharmProjects\Imouto\cog_rcon.py", line 268, in debug
sys.exit()
SystemExit
Task was destroyed but it is pending!
task: <Task pending name='Task-150' coro=<Server_Bridge.cycle_reconnect() done, defined at C:\Users\lkapp\PycharmProjects\Imouto\cog_rcon.py:169> wait_for=<Future pending cb=[Task.task_wakeup()]> cb=[_chain_future.<locals>._call_set_state() at C:\Users\lkapp\AppData\Local\Programs\Python\Python310\lib\asyncio\futures.py:392]>
Process finished with exit code 0
重新啟動 python 腳本的正確方法是
os.execv(sys.executable, ['python'] + sys.argv)
不是'py'
。
或者,如果您可能有多個 Python 安裝,請更具體地使用
os.execv(sys.executable, ['python3'] + sys.argv) # for python3
或者,例如,
os.execv(sys.executable, ['python3.10'] + sys.argv) # for python 3.10
希望這有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.