Cara menggunakan thread in php w3schools
Is there a realistic way of implementing a multi-threaded model in PHP whether truly, or just simulating it. Some time back it was suggested that you could force the operating system to load another instance of the PHP executable and handle other simultaneous processes. Show The problem with this is that when the PHP code finished executing the PHP instance remains in memory because there is no way to kill it from within PHP. So if you are simulating several threads you can imagine whats going to happen. So I am still looking for a way multi-threading can be done or simulated effectively from within PHP. Any ideas?
Lance Roberts 21.9k30 gold badges108 silver badges129 bronze badges asked Sep 16, 2008 at 9:56
Steve ObbayiSteve Obbayi 5,7955 gold badges26 silver badges24 bronze badges 5 Multi-threading is possible in phpYes you can do multi-threading in PHP with pthreads From the PHP documentation:
Simple Test
First Run
Second Run
Real World Example
answered Mar 19, 2013 at 13:55
19 why don't you use popen?
answered Dec 3, 2010 at 22:27
masterbmasterb 6835 silver badges2 bronze badges 9 Threading isn't available in stock PHP, but concurrent programming is possible by using HTTP requests as asynchronous calls. With the curl's timeout setting set to 1 and using the same session_id for the processes you want to be associated with each other, you can communicate with session variables as in my example below. With this method you can even close your browser and the concurrent process still exists on the server. Don't forget to verify the correct session ID like this:
startprocess.php
process1.php
verifysession.php
closeprocess.php
5 While you can't thread, you do have some degree of process control in php. The two function sets that are useful here are: Process control functions http://www.php.net/manual/en/ref.pcntl.php POSIX functions http://www.php.net/manual/en/ref.posix.php You could fork your process with pcntl_fork - returning the PID of the child. Then you can use posix_kill to despose of that PID. That said, if you kill a parent process a signal should be sent to the child process telling it to die. If php itself isn't recognising this you could register a function to manage it and do a clean exit using pcntl_signal. answered Sep 16, 2008 at 10:30
J.D. Fitz.GeraldJ.D. Fitz.Gerald 2,9172 gold badges18 silver badges17 bronze badges 2 I know this is an old question but for people searching, there is a PECL extension written in C that gives PHP multi-threading capability now, it's located here https://github.com/krakjoe/pthreads
answered Sep 19, 2012 at 2:39
JasonDavisJasonDavis 47.2k97 gold badges305 silver badges526 bronze badges 1 You can use exec() to run a command line script (such as command line php), and if you pipe the output to a file then your script won't wait for the command to finish. I can't quite remember the php CLI syntax, but you'd want something like:
I think quite a few shared hosting servers have exec() disabled by default for security reasons, but might be worth a try.
Anil 21.6k9 gold badges75 silver badges99 bronze badges answered Sep 16, 2008 at 14:03
Adam HopkinsonAdam Hopkinson 27.7k7 gold badges64 silver badges95 bronze badges 0 You could simulate threading. PHP can run background processes via popen (or proc_open). Those processes can be communicated with via stdin and stdout. Of course those processes can themselves be a php program. That is probably as close as you'll get. answered Aug 26, 2010 at 21:33
PetePete 4094 silver badges8 bronze badges You can have option of:
Joe McBride 3,7692 gold badges32 silver badges37 bronze badges answered May 10, 2011 at 7:40
How about pcntl_fork? check our the manual page for examples: PHP pcntl_fork
answered Jan 13, 2012 at 0:55
JarrodJarrod 9,1915 gold badges57 silver badges72 bronze badges If you are using a Linux server, you can use
If you need pass some args
In script.php
Or use Symfony https://symfony.com/doc/current/components/process.html
answered Apr 29, 2020 at 15:12
Юрий ЯрвиненЮрий Ярвинен 1911 gold badge4 silver badges7 bronze badges Depending on what you're trying to do you could also use curl_multi to achieve it. answered Jan 25, 2011 at 4:45
SheldmanduSheldmandu 1,67016 silver badges10 bronze badges
Palec 11.9k7 gold badges59 silver badges129 bronze badges answered Feb 2, 2012 at 4:07
StileroStilero 4594 silver badges18 bronze badges 0 I know this is an old question, but this will undoubtedly be useful to many: PHPThreads Code Example:
Requires PHP extensions:
I've been using this library in production now for months. I put a LOT of effort into making it feel like using POSIX pthreads. If you're comfortable with pthreads, you can pick this up and use it very effectively in no time. Computationally, the inner workings are quite different, but practically, the functionality is nearly the same including semantics and syntax. I've used it to write an extremely efficient WebSocket server that supports high throughput rates. Sorry, I'm rambling. I'm just excited that I finally got it released and I want to see who it will help! answered Mar 25 at 13:37
Justin JackJustin Jack 5057 silver badges13 bronze badges 1 Multithreading means performing multiple tasks or processes simultaneously, we can achieve this in php by using following code,although there is no direct way to achieve multithreading in php but we can achieve almost same results by following way.
} Test_1.php
This will execute test_1.php two times simultaneously and both process will run in the background simultaneously ,so in this way you can achieve multithreading in php. This guy done really good work Multithreading in php answered Nov 5, 2013 at 12:51
Pir AbdulPir Abdul 2,1541 gold badge23 silver badges35 bronze badges 3 As of the writing of my current comment, I don't know about the PHP threads. I came to look for the answer here myself, but one workaround is that the PHP program that receives the request from the web server delegates the whole answer formulation to a console application that stores its output, the answer to the request, to a binary file and the PHP program that launched the console application returns that binary file byte-by-byte as the answer to the received request. The console application can be written in any programming language that runs on the server, including those that have proper threading support, including C++ programs that use OpenMP. One unreliable, dirty, trick is to use PHP for executing a console application, "uname",
and print the output of that console command to the HTML output to find out the exact version of the server software. Then install the exact same version of the software to a VirtualBox instance, compile/assemble whatever fully self-contained, preferably static, binaries that one wants and then upload those to the server. From that point onwards the PHP application can use those binaries in the role of the console application that has proper multi-threading. It's a dirty, unreliable, workaround to a situation, when the server administrator has not installed all needed programming language implementations to the server. The thing to watch out for is that at every request that the PHP application receives the console application(s) terminates/exit/get_killed. As to what the hosting service administrators think of such server usage patterns, I guess it boils down to culture. In Northern Europe the service provider HAS TO DELIVER WHAT WAS ADVERTISED and if execution of console commands was allowed and uploading of non-malware files was allowed and the service provider has a right to kill any server process after a few minutes or even after 30 seconds, then the hosting service administrators lack any arguments for forming a proper complaint. In United States and Western Europe the situation/culture is very different and I believe that there's a great chance that in U.S. and/or Western Europe the hosting service provider will refuse to serve hosting service clients that use the above described trick. That's just my guess, given my personal experience with U.S. hosting services and given what I have heard from others about Western European hosting services. As of the writing of my current comment(2018_09_01) I do not know anything about the cultural norms of the Southern-European hosting service providers, Southern-European network administrators. answered Sep 1, 2018 at 5:04
Not the answer you're looking for? Browse other questions tagged php multithreading or ask your own question. |