


The example in this article describes the implementation method of multi-thread concurrency in PHP. Share it with everyone for your reference, the details are as follows:
Multi-threading in Java is a new thread thing. PHP relies on Apache and there is a multi-threading method at the bottom of Linux.
Here is how to simulate PHP concurrency if you cannot control the Apache server
<?php if(function_exists('date_default_timezone_set')) { date_default_timezone_set('PRC'); } function a() { $time = time(); sleep(3); $fp = fopen('result_a'.$time.'.log', 'w'); fputs($fp, 'Set in ' . Date('h:i:s', time()) . (double)microtime() . "rn"); fclose($fp); } function b() { $time = time(); sleep(3); $fp = fopen('result_b'.$time.'.log', 'w'); fputs($fp, 'Set in ' . Date('h:i:s', time()) . (double)microtime() . "rn"); fclose($fp); } if(!isset($_GET['act'])) $_GET['act'] = 'a'; if($_GET['act'] == 'a') { a(); } else if($_GET['act'] == 'b') b(); ?>
The above code writes a file locally.
If you visit localhost/a.php and open two browser tabs at the same time as quickly as possible, you will find that the difference in creation time of the two files is 3 seconds
But if you visit localhost/a.php?act=b another one Visit /a.php?act=a and you will find that the two files were created at almost the same time.
For apache, the same URL means a thread (or process), but different URLs mean concurrency.
If there is a download action inside php
function runThread() { down("http://localhost/test/a.php?act=a"); } if($_GET['act'] == 'run') { echo 'start:'; runThread(); echo ' End'; }
http://localhost/test/a.php?act=run
http://localhost/test/a.php?act=run&s=2
As long as the host If the accessed URLs are different, they are considered to be different processes, which means concurrency. The file creation time is not 3 seconds
Friends who have a local Linux server can also use Linux to simulate concurrency
<?php for ($i=0;$i<10;$i++) { echo $i; sleep(5); } ?>
Save the above as test.php, and then write a SHELL code
#!/bin/bash for i in 1 2 3 4 5 6 7 8 9 10 do php -q test.php & done
More PHP will convert the entire website For a summary of methods for generating pure static HTML web pages, please pay attention to the PHP Chinese website for related articles!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
