启动selenium服务器命令会阻止python代码的执行

2024-04-26 13:20:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用python(subprocess.popen)启动selenium服务器命令,然后在该服务器上执行一些其他命令:

subprocess.popen(selenium Server command)

subprocess.popen(next command which should be launched while command selenium server is launched)

问题是第一个命令不会终止,除非第二个命令终止,而且由于python一直在等待第一个命令终止我的代码块,所以第二个命令永远不会启动

我应该使用多线程来实现这一点,还是有其他解决方案?既然我以前从未使用过多线程,我该怎么做呢?你知道吗

谢谢


Tags: 命令服务器whichserverisseleniumbecommand
1条回答
网友
1楼 · 发布于 2024-04-26 13:20:04

在后台启动第一个命令。然后您可以在程序中与它交互,或者启动另一个客户端来进行交互。你知道吗

(假设是Linux)

import subprocess, time

procs = []
procs.append( subprocess.Popen(
    'sleep 3 ; echo first &',
    shell=True) )

procs.append( subprocess.Popen(
    '/bin/echo beer',
    shell=True) )

print '\nwaiting for workers:'
for proc in procs:
    print proc.communicate()

样本输出。注意,beer出现在first之前,第一个shell命令在执行输出之前等待了三秒钟。你知道吗

waiting for workers:
beer
first
(None, None)
(None, None)

相关问题 更多 >