一、subprocess如何设置命令超时时间

最近遇到一个问题,就是我需要在服务器上执行某些shell命令,但是有些命令失败的时候是不会自动终止的,只会一直停在那里,很耗时间。

因此想到了设置超时时间,而 subprocess 模块是没有超时功能的。至于为什么不用其他模块执行shell命令,因为subprocess比较安全。

这时我一开始想到的是使用 multiprocessing.Process 进程的守护进程去实现的,但是发现还是很low而且实现不好。

后面就突然想到了定时器,线程模块有,于是:

import subprocess
from threading import Timer def kill_command(p):
"""终止命令的函数"""
p.kill() def execute(command, timeout):
# 执行shell命令
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # 设置定时器去终止这个命令
timer = Timer(timeout, kill_command, [p]) try:
timer.start()
stdout, stderr = p.communicate()
return_code = p.returncode
print(return_code)
print(stdout)
print(stdout)
except Exception as ex:
print(ex)
finally:
timer.cancel()

subprocess如何设置命令超时时间的更多相关文章

  1. C# 的tcp Socket设置自定义超时时间

    简单的c# TCP通讯(TcpListener) C# 的TCP Socket (同步方式) C# 的TCP Socket (异步方式) C# 的tcp Socket设置自定义超时时间 C# TCP ...

  2. 【开源项目13】Volley框架 以及 设置request超时时间

    Volley提供了优美的框架,使android程序网络访问更容易.更快. Volley抽象实现了底层的HTTP Client库,我们不需关注HTTP Client细节,专注于写出更加漂亮.干净的RES ...

  3. 转 HttpClient 设置连接超时时间

    要: HttpClient 4.5版本升级后,设置超时时间的API又有新的变化,请大家关注. HttpClient升级到4.5版本后,API有很多变化,HttpClient 4之后,API一直没有太稳 ...

  4. HttpClient设置连接超时时间

    https://www.cnblogs.com/winner-0715/p/7087591.html 使用HttpClient,一般都需要设置连接超时时间和获取数据超时时间.这两个参数很重要,目的是为 ...

  5. WebLogic如何设置session超时时间

    1.web.xml  设置WEB应用程序描述符web.xml里的<session-timeout>元素.这个值以分钟为单位,并覆盖weblogic.xml中的TimeoutSecs属性   ...

  6. Mybatis设置sql超时时间

    开始搭建项目框架的时候,忽略了sql执行超时时间的问题. 原本使用.net开发是,默认的超时时间是30s,这个时间一般一般sql是用不到的,但也不排除一些比较复杂或数据量较大的sql. 而java中, ...

  7. WebSphere设置会话超时时间

    WebSphere Application Server的会话超时时间可以在三个层面进行设置,分别为:应用程序服务器级别.应用程序级别和代码层面进行设置. 设置方式:应用程序级别级别和应用级别可以通过 ...

  8. 【轮询】【ajax】【js】【spring boot】ajax超时请求:前端轮询处理超时请求解决方案 + spring boot服务设置接口超时时间的设置

    场景描述: ajax设置timeout在本机测试有效,但是在生产环境等外网环境无效的问题 1.ajax的timeout属性设置 前端请求超时事件[网络连接不稳定时候,就无效了] var data = ...

  9. integrator.setTimeout 设置一个超时时间,超过这个时间之后,扫描的 Activity 将会被 finish 。

    integrator.setTimeout 设置一个超时时间,超过这个时间之后,扫描的 Activity 将会被 finish . +++++++++++++++++++ 经查,没有这个功能

随机推荐

  1. 牛客网多校第5场 H subseq 【树状数组+离散化】

    题目:戳这里 学习博客:戳这里 题意:给n个数为a1~an,找到字典序第k小的序列,输出该序列所有数所在位置. 解题思路:先把所有序列预处理出来,方法是设一个数组为dp,dp[i]表示以i为开头的序列 ...

  2. Spring应用上下文生命周期

    Spring应用上下文生命周期整体分成四个阶段 ConfigurableApplicationContext#refresh,加载或者刷新持久化配置 ConfigurableApplicationCo ...

  3. Ubuntu16安装chrome

    不免让您失望, 安装正常的chrome,Dependency is not satisfiable: libnss3 (>= 2:3.22)问题一直没能解决,故使用chromium次而代之. s ...

  4. sentry can not delete release bug

    sentry can not delete release bug bug $ ./node_modules/@sentry/cli/bin/sentry-cli releases list $ ./ ...

  5. How to get the real screen size(screen resolution) by using js

    How to get the real screen size(screen resolution) by using js 获取用户屏幕的真实像素分辨率, 屏幕实际尺寸 window.deviceP ...

  6. vue 的 computed 属性在什么时间执行

    vue 的 computed 属性在什么时间执行

  7. 最新 Apple iPhone 12 价格 All In One

    最新 Apple iPhone 12 价格 All In One 美版价格 Apple iPhone 12 mini $699 Apple iPhone 12 $799 Apple iPhone 12 ...

  8. HTML5 QRCode Scaner

    HTML5 QRCode Scaner how to scan QR Code using the camera of the phone or website live demo https://c ...

  9. flex & flex-wrap

    flex & flex-wrap https://css-tricks.com/almanac/properties/f/flex-wrap/ https://developer.mozill ...

  10. SpringBoot以war包形式部署到外部Tomcat

    SpringBoot 项目打包时能打成 .jar 与 .war包文件,.jar使用 java -jar xx.jar 就可以启动,而 .war 可以部署到tomcat的 webapps 中,随tomc ...