phantomjs不关闭并离开孤立进程 [英] phantomjs not closing and leaving orphan processes

查看:317
本文介绍了phantomjs不关闭并离开孤立进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PhantomJS 1.9.2,ubuntu 12 LTS和Ghostdirver 1.04以及硒2.35上,经过测试,我得到了悬挂的phantomjs进程.有谁知道解决此问题的好方法?

On PhantomJS 1.9.2, ubuntu 12 LTS and Ghostdirver 1.04 together with selenium 2.35 I get dangling phantomjs processes after my tests. Anyone knows a good way how to fix this?

这是一个演示奇怪行为的测试程序:

Here is a test program that demonstrates the odd behavior:

package testing;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

public class PhantomIsNotKilledDemo {

    private static WebDriver getDriver(){
        String browserPathStr = System.getProperty("selenium.pathToBrowser");
        if (browserPathStr == null) browserPathStr = "/home/user1/apps/phantomjs/bin/phantomjs";

        DesiredCapabilities caps = DesiredCapabilities.phantomjs();

        caps.setCapability("takesScreenshot", true);
        caps.setCapability(
                PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
                browserPathStr );

        WebDriver driver = new PhantomJSDriver(caps);

        return driver;
    }

    public static void main(String[] args) {
        int max = 10;
        for (int i = 0; i < max; i++){
            WebDriver d1 = getDriver();
            d1.get("http://www.imdb.com/title/tt1951264");

            System.out.println("done with cycle " + (i+1) +" of "+max);
            d1.close();
            //d1.quit();
        }

        System.out.println("done");
        System.exit(0);
    }
}

要运行此命令,应提供phantomjs二进制文件的路径作为系统属性或相应地设置变量.

To run this, you should supply the path of your phantomjs binary as system property or set the variable accordingly.

运行此命令后,我执行此shell命令

After letting this run I do this shell command

ps -ef | grep phantomjs

并找到10个悬空的phantomjs进程.

and find 10 dangling phantomjs processes.

如果我改用d1.quit(),则最终没有悬空过程.这显然更好,但是我仍然希望使用.close可以得到相同的结果.

If I use d1.quit() instead, I end up with no dangling process. This is clearly better, but still I would have expected to get the same result with .close.

请注意,这是的交叉点 https://github.com/detro/ghostdriver/issues/162#issuecomment-25536311

更新:该帖子根据Richard的建议进行了更改(请参见下文).

Update This post is changed according to Richard's suggestion (see below).

推荐答案

您应该使用quit()而不是close()来终止进程.

You should be using quit() to terminate the process instead of close().

您已经发现,关闭将关闭当前窗口(和浏览器),但不会关闭该进程.如果您要向该流程发送其他命令或要检查该流程,这将很有用.

As you have discovered, close will close the current window (and browser), but it will not shut down the process. This is useful for if you are going to send additional commands to the process or want to inspect the process.

退出是用于当您想要关闭每个窗口并停止该过程时,听起来像您要找的东西.

Quit is for when you want to close every window and stop the process, which sounds like what you are looking for.

文档两种方法为:

关闭()

关闭当前窗口,如果是,则退出浏览器 当前打开的最后一个窗口.

Close the current window, quitting the browser if it's the last window currently open.

quit()

退出此驱动程序,关闭每个关联的窗口.

Quits this driver, closing every associated window.

这篇关于phantomjs不关闭并离开孤立进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆