繁体   English   中英

Python 运行进程不作为子进程

[英]Python run process not as subprocess

我有一个电报机器人,我想用它来启动另一个电报机器人。 你不能在一个 python 脚本中运行两个电报机器人,所以我有两个 python 脚本:main.py 和 gs_main.py。 当我在两个单独的屏幕会话中同时运行它们时,一切都很好。 但是,从 main.py 启动 gs_main.py 让我很头疼。 我的最后一次尝试如下:

在 main.py 中,我有以下命令来启动 gs_main.py:

def gostart(update: Update, context: CallbackContext) -> None:
    user = update.effective_user
    global goserver
    
    if user.id == USER_ID:
        goserver = os.system("./start_goserver.sh")

start_goserver.sh 的内容如下:

cd /home/ubuntu/gosharing
python3 gs_main.py

但是,这似乎将 gs_main.py 作为 main.py 的子进程打开,因为我收到以下错误:

Error while getting Updates: Conflict: terminated by other getUpdates request; make sure that only one bot instance is running
No error handlers are registered, logging exception.
Traceback (most recent call last):
  File "/home/ubuntu/.local/lib/python3.8/site-packages/telegram/ext/updater.py", line 646, in _network_loop_retry
    if not action_cb():
  File "/home/ubuntu/.local/lib/python3.8/site-packages/telegram/ext/updater.py", line 597, in polling_action_cb
    updates = self.bot.get_updates(
  File "/home/ubuntu/.local/lib/python3.8/site-packages/telegram/ext/extbot.py", line 222, in get_updates
    updates = super().get_updates(
  File "/home/ubuntu/.local/lib/python3.8/site-packages/telegram/bot.py", line 130, in decorator
    result = func(*args, **kwargs)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/telegram/bot.py", line 2861, in get_updates
    self._post(
  File "/home/ubuntu/.local/lib/python3.8/site-packages/telegram/bot.py", line 295, in _post
    return self.request.post(
  File "/home/ubuntu/.local/lib/python3.8/site-packages/telegram/utils/request.py", line 361, in post
    result = self._request_wrapper(
  File "/home/ubuntu/.local/lib/python3.8/site-packages/telegram/utils/request.py", line 283, in _request_wrapper
    raise Conflict(message)
telegram.error.Conflict: Conflict: terminated by other getUpdates request; make sure that only one bot instance is running

我的问题是,如何运行 gs_main.py 而不是 main.py 的子进程?

如果您使用 linux,您可以尝试将两个进程都发送到后台。

  1. 开始你的第一个脚本
  2. 按:CTRL+Z
  3. 类型:bg
  4. 开始你的第二个脚本
  5. 按:CTRL+Z
  6. 类型:bg

如果你想用一个简单的命令运行这两个脚本,你可以像这样创建第三个脚本:

import os

os.spawnl(os.P_DETACH, python, 'script_1.py')
os.spawnl(os.P_DETACH, python, 'script_2.py')

您可以在此处找到有关 spawnl function 的更多信息:

Python 3 - 产卵

我手动解耦了两个脚本,如下所示。 在我的 main.py 中,我将命令更改为:

def gostart(update: Update, context: CallbackContext) -> None:
    user = update.effective_user
    global goserver
    
    if user.id == USER_ID:
        os.system("echo 1 > /home/ubuntu/gosharing/run")

然后,在 /home/ubuntu/gosharing 中,我使用以下代码创建了第二个 main.py:

import os

run = 0
prev = 0

while True:
    file = open("run", "rb")
    try:
        run = int(file.readline())
    except:
        pass
    file.close()
    
    if (prev == 0) & (run == 1):
        os.system("pkill -f gs_main.py && python3 gs_main.py > log &")
        print("starting server")
    if (prev == 1) & (run == 0):
        os.system("pkill -f gs_main.py")
        print("killing server")
    
    prev = run

有用:)。 可能不是最优雅的解决方案,但它可以解决问题。

暂无
暂无

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

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