I want to run some functions parallel in the background.
Is there a nice way to do this in Symfony2 or even with php?
Or is there only the Symfony\Component\Process\Process?
Can I use this Process with a function? I need the actual Context (logged in user and some session data), so it is not possible to source the function out to an external php-file...
Symfony2 Process component allows you to run some shell command or execute php-script in a different process.
To run exact function in a thread try look at PHP Thread class
Related
Fellows,
I want when I do a fileupload on codeigniter when upload finished to run a background process.
The background process will read the file and will insert data on my db. What I want is to the background script (that will be written in php) to be able to use codeignter's configuration and query builder. Because codeingiter's methods are more convenient than uusinhg vanila php.
I know to I can execute stuff on the background via exec and that's now what I am asking. I am asking how the executed script can use codeigniter's methods and libraries.
Check out the documentation on Running via the CLI. It may be possible to pass what they show as a terminal command as the argument to exec and achieve what you want.
I have complex js file that do some heavy calculation and save result to collection.
This written in js because i wan't to avid data transmission.
currently i pass script to mongo shell in this way :
$mongodb < path_to_script
The script consists of several functions?
Is it possible to execute it from PHP?
I saw there is 'nolock' parameter that can be pass to evel method, is it possible to use it when executing from shell or from PHP?
What is consider more safe , using php execute wrapper or executing script from shell ?
When you run a .js file in the mongo shell, it's not running the javascript on the server; it's running it in the shell. Are you using server-side javascript features like db.eval and map/reduce in the mongo shell script?
Either way, I'd suggest forgoing the shell script and server-side javascript functions and either using the aggregation framework for server-side processing or implementing the logic in PHP application code. Server-side javascript has serious performance and security limitations and it's best to avoid using it when possible.
You can read the js file and execute it in PHP using execute which is a wrapper to the eval command. The nolock option can be added in the arguments.
$code = file_get_contents('filenam.js');
$db->execute($code, array('nolock' => true));
The php execute wrapper should be safer and better solution. If you execute js using shell in PHP, you will need to give permission to php to execute shell script using shell_exec(), which is impossible if you use hosting service and don't have admin right on the host.
Let's say I want to fetch contents from a URL using a PHP script.
One way to do that would be to use PHP function such as
echo file_get_contents("http://www.example.com/file.xml");
Another way would be to use UNIX tools such as wget or curl, or any other tool accessible from shell
echo exec("wget http://www.example.com/file.xml");
Is there a significant performance difference between using exec() and PHP build-in functions to achieve same thing, assuming that both UNIX tools and PHP functions have similar implementation and perform with same efficiency?
What exactly happens when you call an exec() function in terms of resources? Does it actually create a new shell session, or does it run on top of the current php shell session?
The exec() function creates a new shell instance, with its own environment variables, so there is a performance hit.
So, here's my situation :
I have a complete app set up with CodeIgniter.
I'm initiating a background task, running a PHP script.
My question :
How could I use CodeIgniter function (e.g. $this->db or $CI->db - doesn't matter) from that external PHP script?
Idea : Could a workaround, like include 'index.php'; at the top of the script do the trick?
In recent Codeigniter versions you can run standard controller actions from command line.
If your task is some kind of a daemon you can embed the "task trigger" logic in a long running part of your script and shell out for the actual work to CI land.
Is there a way to kill a Windows process, say calc.exe, with PHP without using exec?
exec calls an external program, I try to avoid using that command unless necessary.
There is.
Use the W32api PHP extension, which provides access to the Win32 API - then use the TerminateProcess() API after aquiring a handle to the process.
define new function in PHP sources ( pure C )
compile you new PHP version with the defined function
call the new function, which was defined in PHP sources by you
PS
On this way you are able to make PHP able work with __asm { } insertions/add some kernel code , if you want it...
No, there isn't. PHP has little access to the system except through exec.