Timeout for query execution

1

I have a simple function:

$sql_em = " INSERT INTO tabela ('campo1', 'campo2') VALUES ({$c1}, {$c2}) ";
$em_obj = new Mysqli($this -> host, $this -> db_user, $this -> db_pass, $this -> db_name);

$em_obj -> query($sql_em);

I would like to set timeout to execution of query ( $em_obj -> query($sql_em) ).

Is this possible? If so, does timeout return error?

    
asked by anonymous 09.04.2018 / 13:10

2 answers

0

An idea of how you can do it:

Create a file that will be responsible for the query alone, as if it were to schedule a task.

In the main file, where you do this today, it will do nothing but pass the responsibility onto the new file.

One way to do this:

shell_exec('curl "http://localhost/executaquery.php?dados=" >> /dev/null &' )

This causes the script to run, pass the task, wait for it to return, and continue free.

In your executaquery.php you can use sleep() for the script to run your timeout , it can take as long as you want, the previous process does not depend on it anymore.

That's just a way, maybe even a little nut, to do. It will solve your problem and open your mind to more distributed processes.

Ideally, in this case, you should understand a bit about crontab, to learn ways to schedule tasks in the application. This will help you have queues for any steps in your process.

A practical example would be you want to do hundreds of queries at the same time, without waiting for the previous one to be resolved.

Note that & causes the process to run in the background in the operating system, ie it will stack several processes that will be solved one by one.

for ($i = 1; $i <= 10000; $i++) {
  shell_exec('php query.php &');
}

I talked about this in that answer .

    
09.04.2018 / 15:26
0

The php does not have a timeout function, but the next one is the sleep function.

The function sleep delays script execution:

int sleep ( int $seconds )

Example:

$sql_em = " INSERT INTO tabela ('campo1', 'campo2') VALUES ({$c1}, {$c2}) ";
$em_obj = new Mysqli($this -> host, $this -> db_user, $this -> db_pass, $this -> db_name);

sleep(10);
$em_obj -> query($sql_em);

In this example, the $em_obj -> query($sql_em); line will run after 10 seconds.

    
09.04.2018 / 16:40