EricImprint
EricImprint

Reputation: 177

PHP MySQL Call Procedure and move on

I have a php script I run on my staging machine that processes an xml file into my database then calls some MySQL procedures that are used to clean up the data with a bunch of queries.

I have some procedures that are getting huge and take a few hours to run (which is why it is done in staging).

Is there a way to have PHP call those procedures without waiting for a result to move onto the rest of the script?

Upvotes: 1

Views: 757

Answers (2)

akkie
akkie

Reputation: 2543

PHP doesn't support concurrency in core. But you can simulate a similar behaviour with the following methods.

1. fsockopen() and ignore_user_abort('1')

You can create a socket connection to another script which runs then in background. Please see the following two scripts. The main script creates a socket connection to the sub script and closes it immediately. The sub script runs until all tasks are finished. This is because of the ini value "ignore_user_abort" is set to 1.

main.php

<?php
$start = microtime(true);
$host = 'localhost';
$target = '/sub.php';
$fp = fsockopen($host, 80);
$request  = "GET $target HTTP/1.1\r\n";
$request .= "Host: $host\r\n";
$request .= "Connection: Close\r\n\r\n";
fwrite($fp, $request);
fclose($fp);
file_put_contents("main", "Script runs " . (microtime(true) - $start) . " seconds");

sub.php

<?php
$start = microtime(true);
ignore_user_abort("1");
sleep(5);
file_put_contents("sub", "Script runs " . (microtime(true) - $start) . " seconds");

2. Start a script over the command line in background

You can start a script over the unix/linux or windows command line in background. The following example is for the unix/linux command line. To start the script over the windows command line see the following comment on the PHP.net site. http://www.php.net/manual/en/function.exec.php#110131

main.php

<?php
$start = microtime(true);
pclose(popen("/usr/bin/php "  . __DIR__ . "/sub.php " . __DIR__ . "//sub.log 2>&1 &", 'r'));
file_put_contents("main", "Script runs " . (microtime(true) - $start) . " seconds");

sub.php

<?php
$start = microtime(true);
sleep(5);
file_put_contents("sub", "Script runs " . (microtime(true) - $start) . " seconds");

The following extension provides real concurrency in PHP:

pthreads is an Object Orientated API that allows user-land multi-threading in PHP. It includes all the tools you need to create multi-threaded applications targeted at the Web or the Console. PHP applications can create, read, write, execute and synchronize with Threads, Workers and Stackables.

http://www.php.net/manual/en/book.pthreads.php

Upvotes: 1

Sebas
Sebas

Reputation: 21532

Since you're using a web interface, there's a possibility. You could try to implement the following logic

  1. One php script per "long mysql function"
  2. Use AJAX to process each php script, so you can start any at the same time if you want. The server will parallelise the load.

Tell me what you think.

Upvotes: 1

Related Questions