PHP MongoDb驱动程序:如何设置执行代码的超时 [英] PHP MongoDb driver: How to set timeout for executing a code

查看:140
本文介绍了PHP MongoDb驱动程序:如何设置执行代码的超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,它们在MongoDb的一边执行了一段代码:

I have the following code which executes a piece of code on the MongoDb's side:

$mongoCode = new MongoCode('/* Some JS code */');
$db->execute($mongoCode, array(
    'socketTimeoutMS' => 1000000,
));

如您所见,我试图通过在execute()函数的第二个参数中设置socketTimeoutMS值来设置代码执行的超时时间.但这是行不通的. PHP网站上的文档表明,execute()命令的第二个参数已作为参数发送给代码. 如何为MongoDB::execute()设置超时?请注意,我将MongoDB驱动程序的1.5版用于php和MongoCursor :: $ timeout已弃用,不再起作用.

As you see I have tried to set timeout for the code's execution by setting the socketTimeoutMS value in second parameter of execute() function. But it does not work. Documentations in PHP website indicate that the second parameter of execute() command is sent to code as arguments. How can I set timeout for MongoDB::execute()? Please note that I am using version 1.5 of MongoDB driver for php and MongoCursor::$timeout is deprecated and does not work anymore.

推荐答案

您可以在MongoClient上设置socketTimeoutMS:

You can set the socketTimeoutMS on MongoClient:

$mongo = new MongoClient("mongodb://localhost:27017", 
    array(
        "socketTimeoutMS" => 100000
    )
); 

execute方法的args参数传递给代码,而不传递给驱动程序.

The args parameters for the execute method are passed to the code not to the driver.

您还可以仅在执行命令时设置超时:

You can also set a timeout just when executing the command:

$result = $mongo->dbname->command(
    ['eval' => $code],
    ['socketTimeoutMS' => 1]
);

或者,如果您不执行命令,则可以在光标上设置超时:

Alternatively, if you're not executing commands, you can set the timeout on the cursor:

$cursor = $collection->find([]);
$cursor->timeout(10000);

这显然对execute命令不起作用,因为该命令不会返回游标.

This will obviously not work on the execute command, because that command doesn't return a cursor.

这篇关于PHP MongoDb驱动程序:如何设置执行代码的超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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