这篇文章主要介绍了PHP+shell实现多线程的方法,实例分析了php实现多线程的相关技巧,需要的朋友可以参考下
本文实例讲述了PHP+shell实现多线程的方法。分享给大家供大家参考。具体如下:
这里介绍如何借助shell脚本实现多线程。
先写个简单的php代码,这里为了让脚本执行时间更长,,方便看效果,sleep一下,呵呵!先看下test.php的代码:
PHP代码:
在看下shell脚本的代码,非常简单
#!/bin/bash for i in 1 2 3 4 5 6 7 8 9 10 do /usr/bin/php -q /var/www/html/test.php & done
注意到在请求php代码的那行有一个&符号吗,这个是关键,不加的话是不能进行多线程的,&表示讲服务推送到后台执行,因此,在 shell的每次的循环中不必等php的代码全部执行完在请求下一个文件,而是同时进行的,这样就实现了多线程,下面运行下shell看下效果,这里你将 看到10个test.php进程再跑,再利用linux的定时器,定时请求这个shell,在处理一些需要多线程的任务,例如,批量下载时,非常好用!
希望本文所述对大家的php程序设计有所帮助。

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

Zend Studio 13.0.1
Powerful PHP integrated development environment

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.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)