Recently I was working on a project that required the use of asynchronous PHP. When I was looking through the PHP source code, I found three unused modules, sysvsem, sysvshm, and sysvmsg. After some research, I benefited a lot.
There is such a family of functions in PHP, which are packages of the V IPC function family of UNIX.
They are rarely used by people, but they are very powerful. Using them wisely can get you twice the result with half the effort.
They include:
Semaphores
##Shared Memory
Inter-Process Messaging (IPC)
Based on these, it is entirely possible for us to package PHP into a message-driven system. But, first, we need to cover a few important basics: 1. ftokint ftok ( string pathname, string proj )
//ftok将一个路径名pathname和一个项目名(必须为一个字符), 转化成一个整形的用来使用系统V IPC的key
2. ticksTicks is from PHP 4.0. It was only added to PHP at the beginning of 3. It is an event that occurs every time the interpreter executes N low-level statements in the declare code segment. The value of N is specified with ticks=N in the directive part of declare. function getStatus($arg){ print_r connection_status(); debug_print_backtrace(); } reigster_tick_function("getStatus", true); declare(ticks=1){ for($i =1; $i<999; $i++){ echo "hello"; } } unregister_tick_function("getStatus");This is basically equivalent to:
function getStatus($arg){ print_r connection_status(); debug_print_backtrace(); } reigster_tick_function("getStatus", true); declare(ticks=1){ for($i =1; $i<999; $i++){ echo "hello"; getStatus(true); } } unregister_tick_function("getStatus");Message. I will now use an example to illustrate how to combine Ticks to implement PHP message communication.
$mesg_key = ftok(__FILE__, 'm'); $mesg_id = msg_get_queue($mesg_key, 0666); function fetchMessage($mesg_id){ if(!is_resource($mesg_id)){ print_r("Mesg Queue is not Ready"); } if(msg_receive($mesg_id, 0, $mesg_type, 1024, $mesg, false, MSG_IPC_NOWAIT)){ print_r("Process got a new incoming MSG: $mesg "); } } register_tick_function("fetchMessage", $mesg_id); declare(ticks=2){ $i = 0; while(++$i < 100){ if($i%5 == 0){ msg_send($mesg_id, 1, "Hi: Now Index is :". $i); } } } //msg_remove_queue($mesg_id);In this example, first add our PHP execution Process to a message queue obtained by the Key generated by ftok. Then, through Ticks, query the message queue once every two statements. Then simulated message sending. Access this script in the browser, the result is as follows:
Process got a new incoming MSG: s:19:"Hi: Now Index is :5"; Process got a new incoming MSG: s:20:"Hi: Now Index is :10"; Process got a new incoming MSG: s:20:"Hi: Now Index is :15"; Process got a new incoming MSG: s:20:"Hi: Now Index is :20"; Process got a new incoming MSG: s:20:"Hi: Now Index is :25"; Process got a new incoming MSG: s:20:"Hi: Now Index is :30"; Process got a new incoming MSG: s:20:"Hi: Now Index is :35"; Process got a new incoming MSG: s:20:"Hi: Now Index is :40"; Process got a new incoming MSG: s:20:"Hi: Now Index is :45"; Process got a new incoming MSG: s:20:"Hi: Now Index is :50"; Process got a new incoming MSG: s:20:"Hi: Now Index is :55"; Process got a new incoming MSG: s:20:"Hi: Now Index is :60"; Process got a new incoming MSG: s:20:"Hi: Now Index is :65"; Process got a new incoming MSG: s:20:"Hi: Now Index is :70"; Process got a new incoming MSG: s:20:"Hi: Now Index is :75"; Process got a new incoming MSG: s:20:"Hi: Now Index is :80"; Process got a new incoming MSG: s:20:"Hi: Now Index is :85"; Process got a new incoming MSG: s:20:"Hi: Now Index is :90"; Process got a new incoming MSG: s:20:"Hi: Now Index is :95";Seeing this, everyone already has a concept of how to simulate PHP as event-driven? Don't worry, we will continue to improve. 3. SemaphoreEveryone should be familiar with the concept of semaphore. Through semaphores, process communication, competition, etc. can be achieved. I won’t go into details again, but simply list the set of semaphore functions provided in PHP.
sem_acquire -- Acquire a semaphore sem_get -- Get a semaphore id sem_release -- Release a semaphore sem_remove -- Remove a semaphoreFor specific information, you can read the PHP manual. 4. Memory sharingPHP sysvshm provides a memory sharing solution: sysvshm, which is in the same series as sysvsem and sysvmsg, but here, I did not use it, I used shmop series functions, run this example in conjunction with TIcks
function memoryUsage(){ printf("%s: %s<br/>", date("H:i:s", $now), memory_get_usage()); //var_dump(debug_backtrace()); //var_dump(__FUNCTION__); //debug_print_backtrace(); } register_tick_function("memoryUsage"); declare(ticks=1){ $shm_key = ftok(__FILE__, 's'); $shm_id = shmop_open($shm_key, 'c', 0644, 100); } printf("Size of Shared Memory is: %s<br/>", shmop_size($shm_id)); $shm_text = shmop_read($shm_id, 0, 100); eval($shm_text); if(!empty($share_array)){ var_dump($share_array); $share_array['id'] += 1; }else{ $share_array = array('id' => 1); } $out_put_str = "$share_array = " . var_export($share_array, true) .";"; $out_put_str = str_pad($out_put_str, 100, " ", STR_PAD_RIGHT); shmop_write($shm_id, $out_put_str, 0); ?>, and continuously refresh, we can see that the index is increasing. Just using this shmop can complete the functions of sharing data between PHP scripts: as well as, such as caching, counting, etc. For more PHP related knowledge, please visit
PHP Chinese website!
The above is the detailed content of What you may not know about PHP – PHP's event-driven design. For more information, please follow other related articles on the PHP Chinese website!

如何在PHP后端功能开发中合理应用设计模式?设计模式是一种经过实践证明的解决特定问题的方案模板,可以用于构建可复用的代码,在开发过程中提高可维护性和可扩展性。在PHP后端功能开发中,合理应用设计模式可以帮助我们更好地组织和管理代码,提高代码质量和开发效率。本文将介绍常用的设计模式,并给出相应的PHP代码示例。单例模式(Singleton)单例模式适用于需要保

如何通过编写代码来学习和运用PHP8的设计模式设计模式是软件开发中常用的解决问题的方法论,它可以提高代码的可扩展性、可维护性和重用性。而PHP8作为最新版的PHP语言,也引入了许多新特性和改进,提供更多的工具和功能来支持设计模式的实现。本文将介绍一些常见的设计模式,并通过编写代码来演示在PHP8中如何运用这些设计模式。让我们开始吧!一、单例模式(Sing

本篇文章给大家带来了关于golang设计模式的相关知识,其中主要介绍了职责链模式是什么及其作用价值,还有职责链Go代码的具体实现方法,下面一起来看一下,希望对需要的朋友有所帮助。

随着数据的增长和复杂性的不断提升,ETL(Extract、Transform、Load)已成为数据处理中的重要环节。而Go语言作为一门高效、轻量的编程语言,越来越受到人们的热捧。本文将介绍Go语言中常用的ETL设计模式,以帮助读者更好地进行数据处理。一、Extractor设计模式Extractor是指从源数据中提取数据的组件,常见的有文件读取、数据库读取、A

单例模式是一种常见的设计模式,它在系统中仅允许创建一个实例来控制对某些资源的访问。在 Go 语言中,实现单例模式有多种方式,本篇文章将带你深入掌握 Go 语言中的单例模式实现。

随着JavaScript的不断发展和应用范围的扩大,越来越多的开发人员开始意识到设计模式和最佳实践的重要性。设计模式是一种被证明在某些情况下有用的软件设计解决方案。而最佳实践则是指在编程过程中,我们可以应用的一些最佳的规范和方法。在本文中,我们将探讨JavaScript中的设计模式和最佳实践,并提供一些具体的代码示例。让我们开始吧!一、JavaScript中

设计模式的六大原则:1、单一职责原则,其核心就是控制类的粒度大小、将对象解耦、提高其内聚性;2、开闭原则,可以通过“抽象约束、封装变化”来实现;3、里氏替换原则,主要阐述了有关继承的一些原则;4、依赖倒置原则,降低了客户与实现模块之间的耦合;5、接口隔离原则,是为了约束接口、降低类对接口的依赖性;6、迪米特法则,要求限制软件实体之间通信的宽度和深度。

探索Java开发中的设计模式经验与建议设计模式是软件开发中用于解决特定问题的一种面向对象的可复用解决方案。在Java开发中,设计模式是很重要的一部分,它能够提高代码的可读性和可维护性,并且能够加速开发过程。通过运用设计模式,开发人员可以更好地组织和管理代码,同时也能够避免一些常见的开发错误。在Java开发中,有很多常用的设计模式,如单例模式、工厂模式、观察者


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

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 English version
Recommended: Win version, supports code prompts!

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools