- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 python 并行启动两个子进程。一个是 HTTP 服务器,而另一个是另一个程序的执行(CustomSimpleHTTPServer.py,它是由 selenium IDE 插件生成的 python 脚本,用于打开 firefox,导航到网站并进行一些交互)。另一方面,我想在第二个子进程完成执行时停止执行第一个子进程(HTTP 服务器)。
我的代码逻辑是 selenium 脚本将打开一个网站。该网站将自动对我的 HTTP 服务器进行一些 GET 调用。 selenium 脚本执行完毕后,应该关闭 HTTP 服务器,以便它可以将所有捕获的请求记录在一个文件中。
这是我的主要 Python 代码:
class Myclass(object):
HTTPSERVERPROCESS = ""
def startHTTPServer(self):
print "********HTTP Server started*********"
try:
self.HTTPSERVERPROCESS=subprocess.Popen('python CustomSimpleHTTPServer.py', \
shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
except Exception as e:
print "Exception captured while starting HTTP Server process: %s\n" % e
def startNavigatingFromBrowser(self):
print "********Opening firefox to start navigation*********"
try:
process=subprocess.Popen('python navigationScript.py', \
shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
process.communicate()
process.wait()
except Exception as e:
print "Exception captured starting Browser Navigation process : %s\n" % e
try:
if process.returncode==0:
print "HTTPSERVEPROCESS value: %s" % self.HTTPSERVERPROCESS.returncode
print self.HTTPSERVERPROCESS
#self.HTTPSERVERPROCESS.kill()
#self.HTTPSERVERPROCESS.terminate()
#self.kill(self.HTTPSERVERPROCESS.pid)
except Exception as e:
print "Exception captured while killing HTTP Server process : %s\n" % e
def kill(self,proc_pid):
process = psutil.Process(proc_pid)
for proc in process.get_children(recursive=True):
proc.kill()
process.kill()
def startCapture(self):
print "********Starting Parallel execution of Server initiation and firefox navigation script*********"
t1 = threading.Thread(target=self.startHTTPServer())
t2 = threading.Thread(target=self.startNavigatingFromBrowser())
t1.start()
t2.start()
t2.join()
注意:通过调用 startCapture() 开始执行
这是 CustomSimpleHTTPServer.py 的代码,它应该在终止时将捕获的请求写入 logfile.txt:
import SimpleHTTPServer
import SocketServer
PORT = 5555
class MyHTTPHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
log_file = open('logfile.txt', 'w')
def log_message(self, format, *args):
self.log_file.write("%s - - [%s] %s\n" %
(self.client_address[0],
self.log_date_time_string(),
format%args))
Handler = MyHTTPHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
httpd.serve_forever()
当我使用 self.HTTPSERVERPROCESS.kill() 或 self.HTTPSERVERPROCESS.terminate() 或 os.kill() 时,我在运行主要 Python 代码时在我的终端中得到跟随
********Starting Parallel execution of Server initiation and firefox navigation script*********
********SimpleHTTPServer started*********
********Opening firefox to start navigation*********
HTTPSERVEPROCESS value: <subprocess.Popen object at 0x1080f8410>
2459
Exception captured while killing HTTP Server process : [Errno 3] No such process
Process finished with exit code 0
当我使用 self.kill(self.HTTPSERVERPROCESS.pid) 时,我会在运行主要 Python 代码时在我的终端中得到关注
********Starting Parallel execution of Server initiation and firefox navigation script*********
********SimpleHTTPServer started*********
********Opening firefox to start navigation*********
HTTPSERVEPROCESS value: <subprocess.Popen object at 0x1080f8410>
2459
Exception captured while killing HTTP Server process : 'Process' object has no attribute 'get_children'
Process finished with exit code 0
以下 3 个都不能终止 HTTPServer 进程:
self.HTTPSERVERPROCESS.kill()
self.HTTPSERVERPROCESS.terminate()
self.kill(self.HTTPSERVERPROCESS.pid)
我知道 CustomSimpleHTTPServer.py 是正确的,因为当我单独运行它并手动浏览到网站,然后通过在终端中按 CTRL-c 手动终止 CustomSimpleHTTPServer.py 脚本时,日志将填充到 logfle.txt 中。
我应该对我的代码进行哪些更改才能使其正常工作并填充日志?
最佳答案
你应该只使用 os.kill()
来通知进程:
import os
import signal
...
os.kill(the_pid, signal.SIGTERM) # usually kills processes
os.kill(the_pid, signal.SIGKILL) # should always kill a process
此外,如果您杀死父进程,它通常也会杀死子进程。
更新:
我对服务器程序做了两个小改动:
self.log_file.flush()
的调用以确保日志条目被刷新到日志文件。allow_reuse_address
以便您可以重用终止服务器后不久的相同地址。(参见 this SO question)文件服务器
:
#!/usr/bin/env python
import SimpleHTTPServer
import SocketServer
PORT = 5555
class MyServer(SocketServer.TCPServer):
allow_reuse_address = True
class MyHTTPHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
log_file = open('logfile.txt', 'w')
def log_message(self, format, *args):
self.log_file.write("%s - - [%s] %s\n" %
(self.client_address[0],
self.log_date_time_string(),
format%args))
self.log_file.flush()
Handler = MyHTTPHandler
httpd = MyServer(("", PORT), Handler)
httpd.serve_forever()
这是一个简单的导航程序(文件 Navigate
):
#!/usr/bin/env python
import requests
import time
URL = "http://localhost:5555/"
def main():
for x in range(5):
print "x =", x
r = requests.get(URL + "asd")
time.sleep(1)
print "Quitting"
main()
这是主程序:
#!/usr/bin/env python
import os
import subprocess
import signal
def main():
# start web server
web = subprocess.Popen(["./Server"])
print "web server pid:", web.pid
# start navigator
nav = subprocess.Popen(["./Navigate"])
print "nav pid: ", nav.pid
# wait for nav to exit
nav.wait()
print "done waiting for nav"
print "killing web server"
os.kill(web.pid, signal.SIGTERM )
web.wait()
print "server terminated"
main()
关于python - 无法使用 process.kill() 或 process.terminate() 或 os.kill() 或使用 psutil 终止 Python 子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38726804/
一个观察:当我在 android 上开发我的应用程序时,我注意到 LogCat 中有以下两行。这些发生是因为我自己的应用程序的内存需求太多。我读过 Android 可以决定何时摆脱不需要或由于内存需求
SIGINT有什么区别信号和 SIGTERM信号?我知道SIGINT相当于在键盘上按Ctrl+C,但什么是SIGTERM为了?如果我想优雅地停止一些后台进程,我应该使用哪些? 最佳答案 响应的唯一区别
假设我有一个父进程和一个子进程(以例如 fork() 或 clone() 启动)在 Linux 上运行。进一步假设存在一些父子都可以修改的共享内存。 在父进程的上下文中,我想停止子进程并知道它实际上已
所以我最近了解到 kill is not a synchronous命令,所以我在 bash 中使用这个 while 循环,这很棒: while kill PID_OF_THE_PROCESS 2>/
我遇到了一个问题。我们有一个干净的脚本用来清理旧文件,有时我们需要停止它,稍后再启动它。像下面的过程。我们在check.sh中使用kill -STOP $pid和kill -CONT $pid来控制c
内核中谁负责终止进程。 如果“kill”来了,进程处于阻塞状态怎么办。 kill是否等到进程进入running状态才清理自己。 如果有人可以在内核方面回答更多问题,例如当 kill 命令生成 SIGI
在下面的链接中 http://www.faqs.org/docs/Linux-HOWTO/NFS-HOWTO.html 它说一个进程除非被“sure kill”才能杀死,但是什么是sure kill?
我正在尝试编写一个快速的 bash 脚本,以在脚本检测到的特定条件下向程序发出信号,并且出于习惯,我正在使用某些 bin 实用程序的完整路径,即 /bin/rm 和 /bin/kill 代替 rm 和
我的服务器在端口 80 上运行 nohup。我试试 ps aux | grep nohup 得到 root 9234 0.0 0.1 11740 932 pts/1 S+ 15:19 0:00 gre
我写了下面的脚本,如果 grep 在文件中找不到相关的字符串,它会启用 20 秒的超时。 脚本运行良好,但是脚本的输出是这样的: ./test: line 11: 30039: Killed 如何从
基本上我想要一个 bash 脚本进程,在收到 SIGINT 后,在退出之前杀死它的所有子进程。我读了here使用以下内容: trap "kill -TERM -$$ ; exit 1" INT QUI
我有一个名为 Launcher.sh 的 shell 脚本,它由 java 进程执行。 java进程内部使用ProcessBuilder来执行bash脚本。 在 Launcher.sh 中,我有以下代
上下文 我创建了一个调用 celery 任务的 Django 应用程序,该任务又会生成其他任务并等待它们完成。 工作流程如下: 1) 主要的python/django代码在后台启动一个celery任务
我要输出这条消息/usr/local/ex1.sh: line xxx: Killed ex2.sh >> $LOG_FILE 2>&1到日志文件。 不过 “ex1.sh”输出 /usr/local
我最近上传了很多问题,我认为人们对我感到厌倦,但我不擅长编程,并且正在尝试为 A-level 类(class)编写游戏代码,所以我需要所有可以得到的帮助来学习语言。不管怎样,我将在下面展示一些相关的代
hadoop job -kill job_id 和 yarn application -kill application_id 有什么区别? job_id 和 application_id 是否代表/
在我的 post-command-hook回调,当我这样做时 kill-word , this-command var 是 kill-region - 而不是 kill-word正如预期的那样。 我想
我有一个要在 Oracle Linux OS 上运行的 C++ 应用程序。 考虑一下,我用 new 创建了几个对象。运算符(operator)。虽然我已经使用 delete 操作符来解除分配它,但是
不确定是否相关,但在周末我将我的操作系统升级到 Big Sur 11.1 版,然后当我开始工作时,一件又一件地出错,现在我根本无法使用 node 或 npm。我在 zsh 中收到以下消息: % nod
我使用 python 并行启动两个子进程。一个是 HTTP 服务器,而另一个是另一个程序的执行(CustomSimpleHTTPServer.py,它是由 selenium IDE 插件生成的 pyt
我是一名优秀的程序员,十分优秀!