Timeout for query execution

Asked

Viewed 120 times

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 one timeout for the execution of query ($em_obj -> query($sql_em)).

Is it possible ? If yes, there is return of the timeout error ?

  • I’ve never seen anything like this with php. They want to let the program flow faster the timeout interferes a lot in the script flow perhaps the best way to do this is by controlling these queries on the client side with javascript

  • @Atlasilva the problem is that I run this same query coming from various places, with the same Insert, but some of them, lock in the query. I needed it to debug.

  • understand. Already tried to check operation memory consumption?

  • I even decided to tell the truth, it was an error in the object with commit and another with rollback, but I left the question to see if it exists...

2 answers

0

An idea of how you can do it:

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

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

One of the ways to do:

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

This causes the script to run, pass the task, do not wait for return and stay free.

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

This is just a way, maybe even a little bit dirty, to do. It will solve your problem and open your mind to more distributed processes.

Ideal in this case, is you understand a little 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 to want to do hundreds of queries at the same time, without waiting for the previous one to be solved.

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

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

I talked a little bit about it in that reply.

  • So, but then I don’t have a return because of the timeout, I would just "kill" the processing of PHP and not the correct query !?

  • 2

    Correct, only from PHP. Now I understand your question from another angle, very good question. I will answer soon

  • Your problem seems strange, take a look at these two contents please. Let’s try to understand and then update the answer to help the next.

  • https://stackoverflow.com/questions/5836623/getting-lock-wait-timeout-exceeded-try-restarting-transaction-even-though-im

  • https://stackoverflow.com/questions/415905/how-to-set-a-maximum-execution-time-for-a-mysql-query

0

php does not have a timeout function, but next to that is the function sleep.

The function sleep delays the execution of the script:

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 line $em_obj -> query($sql_em); will be executed after 10 seconds.

  • But I don’t want to "delay" the processing, I wanted to set a timeout time, equal to the set_time_limit(120) php, and if it returns the error, example: (closed by timeout).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.