简体   繁体   English

Python Pycharm 脚本自重启

[英]Python Pycharm Script Self-Restart

I'm making a discord bot and I want to be able to restart it via a command.我正在制作一个 discord 机器人,我希望能够通过命令重新启动它。 I have the command loaded in the main script right below my client events for error, onmessage, etc, as:我在客户端事件正下方的主脚本中加载了用于错误、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

Upon running this by calling )reboot in the discord chat my bot is monitoring, my program exits with exit code 0 and does not restart.通过在 discord 聊天中调用 )reboot 运行此程序后,我的机器人正在监视,我的程序以退出代码 0 退出并且不会重新启动。

Here's the place i found some stuff that didn't work for me: Restart python-script from within itself这是我发现一些对我不起作用的地方: Restart python-script from inside itself

Some stuff, not sure how relevant any of it is:有些东西,不确定其中的相关性如何:

  • There's async stuff going on, so i was wondering if there might be some voodoo code quirks that come into play because of that有异步的东西正在发生,所以我想知道是否可能有一些巫毒代码怪癖因此而发挥作用
  • Running the code from pycharm, which has a venv.从 pycharm 运行代码,它有一个 venv。 I don't really know what that means specifically, but I have an idea from my experience, and maybe it's messing something up idk.我真的不知道这具体意味着什么,但我从我的经验中有一个想法,也许它搞砸了 idk。

Another thing I found, when trying to use exit() to stop the program, it takes awhile and then gives me this error message (apologies for size, no clue what's important and whats not):我发现的另一件事是,当尝试使用 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

The correct way to restart the python script is重新启动 python 脚本的正确方法是

os.execv(sys.executable, ['python'] + sys.argv)

not 'py' .不是'py'

Alternatively, if you might have multiple installations of Python, be more specific by using或者,如果您可能有多个 Python 安装,请更具体地使用

os.execv(sys.executable, ['python3'] + sys.argv)  # for python3

Or, for example,或者,例如,

os.execv(sys.executable, ['python3.10'] + sys.argv)  # for python 3.10

Hope this helped.希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM