最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ubuntu - Timeout when I try an action with Python subprocess but when I run it manually throw terminal i'ts fine - Stack

programmeradmin3浏览0评论

I have this function which is trying to restart a genymotion device in python:

def restart_device(d, stop_timeout=40, start_timeout=30):
    """Restarts a specific Genymotion device with timeouts and logs the process."""
    try:
        device_name = get_device_name_by_ip(d.serial)
        logging.info(f"Stopping device: {device_name}")
        subprocess.run([gmtoolPath, "admin", "stop", device_name], check=True, timeout=stop_timeout)
        time.sleep(5)  # Wait a bit before restarting

        logging.info(f"Starting device: {device_name}")
        subprocess.run([gmtoolPath, "admin", "start", device_name], check=True, timeout=start_timeout)

        # Wait for the device to boot up
        logging.info(f"Waiting for device {device_name} to boot...")
        time.sleep(30)  # Adjust the wait time as needed
        logging.info(f"Device {device_name} restarted successfully.")
    except subprocess.CalledProcessError as e:
        logging.error(f"Error restarting device {device_name}: {e.stderr}")
    except subprocess.TimeoutExpired as e:
        logging.error(f"Timeout expired while restarting device {device_name}: {e}")

When I run the it throw the code its returning timeout but when I run the stop and start commands manually with the termianl its working perfectly.

Also an important note, I have another computer and when I run the exact same code its working fine on it.

Any help will be helpfull :)

I have this function which is trying to restart a genymotion device in python:

def restart_device(d, stop_timeout=40, start_timeout=30):
    """Restarts a specific Genymotion device with timeouts and logs the process."""
    try:
        device_name = get_device_name_by_ip(d.serial)
        logging.info(f"Stopping device: {device_name}")
        subprocess.run([gmtoolPath, "admin", "stop", device_name], check=True, timeout=stop_timeout)
        time.sleep(5)  # Wait a bit before restarting

        logging.info(f"Starting device: {device_name}")
        subprocess.run([gmtoolPath, "admin", "start", device_name], check=True, timeout=start_timeout)

        # Wait for the device to boot up
        logging.info(f"Waiting for device {device_name} to boot...")
        time.sleep(30)  # Adjust the wait time as needed
        logging.info(f"Device {device_name} restarted successfully.")
    except subprocess.CalledProcessError as e:
        logging.error(f"Error restarting device {device_name}: {e.stderr}")
    except subprocess.TimeoutExpired as e:
        logging.error(f"Timeout expired while restarting device {device_name}: {e}")

When I run the it throw the code its returning timeout but when I run the stop and start commands manually with the termianl its working perfectly.

Also an important note, I have another computer and when I run the exact same code its working fine on it.

Any help will be helpfull :)

Share Improve this question asked Feb 16 at 18:23 Achiya Ben NatanAchiya Ben Natan 133 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Try adding shell=True to the arguments in your subprocess.run() calls:

subprocess.run([gmtoolPath, "admin", "stop", device_name], check=True, timeout=stop_timeout, shell=True)

and

subprocess.run([gmtoolPath, "admin", "start", device_name], check=True, timeout=start_timeout, shell=True)

By default, subprocess basically uses its own shell to run commands, but sometimes that means it works a bit differently than the one your computer uses normally. Adding shell=True has it use the OS's normal shell instead, and since the commands work normally when you use the normal shell that should fix it.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论