PHPメッセージキュー
php-メッセージキューを実装し、共有メモリを介して通信を処理する 2 つのクラス
メッセージ キューを実装するには、Apache ActiveMQ、memcacheq... などのより専門的なツールを使用できます。ここでは 2 つの基本的で簡単な実装方法を示します。
memcache メソッドを使用して実現します
<?php /* * @Copyright (c) 2007,上海友邻信息科技有限公司 * @All rights reserved. * * 这个消息队列不是线程安全的,我只是尽量的避免了冲突的可能性。如果你要实现线程安全的,一个建议是通过文件进行锁定,然后进行操作。 * * @filename MemcacheQueue.class.php */ /** * Class and Function List: * Function list: * - __construct() * - singleton() * - init() * - __get() * - __set() * - isEmpty() * - isFull() * - enQueue() * - deQueue() * - getTop() * - getAll() * - getPage() * - makeEmpty() * - getAllKeys() * - add() * - increment() * - decrement() * - set() * - get() * - delete() * - getKeyByPos() * Classes list: * - Yl_MemcacheQueue */ class Yl_MemcacheQueue { private static $instance; private $memcache; private $name; private $prefix; private $maxSize; private function __construct() { } static function singleton() { if (! (self::$instance instanceof self)) { self::$instance = new Yl_MemcacheQueue (); } return self::$instance; } public function init($max_size, $name, $prefix = "__queue__") { $max_size = 1000; $name = '_war_'; $prefix = '_queue'; $this->memcache = Yl_Memcache::singleton (); $this->name = $name; $this->prefix = $prefix; $this->maxSize = $max_size; $this->add ( 'front', 0 ); $this->add ( 'rear', 0 ); $this->add ( 'size', 0 ); } function isEmpty() { return $this->get ( 'size' ) == 0; } function isFull() { return $this->get ( 'size' ) >= $this->maxSize; } function enQueue($data) { if ($this->isFull ()) { throw new Exception ( "Queue is Full" ); } $size = $this->increment ( 'size' ); $rear = $this->increment ( 'rear' ); $this->set ( ($rear - 1) % $this->maxSize, $data ); return $this; } function deQueue() { if ($this->isEmpty ()) { throw new Exception ( "Queue is Empty" ); } $this->decrement ( 'size' ); $front = $this->increment ( 'front' ); $this->delete ( ($front - 1) % $this->maxSize ); return $this; } function getTop() { return $this->get ( $this->get ( 'front' ) % $this->maxSize ); } function getAll() { return $this->getPage (); } function getPage($offset = 0, $limit = 0) { $size = $this->get ( 'size' ); if (0 == $size || $size get ( 'front' ) % $this->maxSize; $rear = $this->get ( 'rear' ) % $this->maxSize; $keys [] = $this->getKeyByPos ( ($front + $offset) % $this->maxSize ); $num = 1; for($pos = ($front + $offset + 1) % $this->maxSize; $pos != $rear; $pos = ($pos + 1) % $this->maxSize) { $keys [] = $this->getKeyByPos ( $pos ); $num ++; if ($limit > 0 && $limit == $num) { break; } } return array_values ( $this->memcache->get ( $keys ) ); } function makeEmpty() { $keys = $this->getAllKeys (); foreach ( $keys as $value ) { $this->delete ( $value ); } $this->delete ( "rear" ); $this->delete ( "front" ); $this->delete ( 'size' ); $this->delete ( "maxSize" ); } private function getAllKeys() { if ($this->isEmpty ()) { return array (); } $keys [] = $this->get ( 'front' ); for($pos = ($this->get ( 'front' ) % $this->maxSize + 1) % $this->maxSize; $pos != $this->get ( 'rear' ) % $this->maxSize; $pos = ($pos + 1) % $this->maxSize) { $keys [] = $pos; } return $keys; } private function add($pos, $data) { $this->memcache->add ( $this->getKeyByPos ( $pos ), $data ); return $this; } private function increment($pos) { return $this->memcache->increment ( $this->getKeyByPos ( $pos ) ); } private function decrement($pos) { $this->memcache->decrement ( $this->getKeyByPos ( $pos ) ); } private function set($pos, $data) { $this->memcache->save ( $data, $this->getKeyByPos ( $pos ) ); return $this; } private function get($pos) { return $this->memcache->get ( $this->getKeyByPos ( $pos ) ); } private function delete($pos) { return $this->memcache->delete ( $this->getKeyByPos ( $pos ) ); } private function getKeyByPos($pos) { return $this->prefix . $this->name . $pos; } } ?>共有メモリキューを使用した実装 クリックして元のアドレスを表示
?
PHP を使用して Linux メッセージ キューを操作し、プロセス間通信を完了します
私たちが開発するシステムをマルチプロセス モードで実行する必要がある場合、プロセス間通信が重要なリンクになります。メッセージキューは、Linux システムにおけるプロセス間通信の方法です。
Linux システムプロセス通信の概念と実装については、http://www.ibm.com/developerworks/cn/linux/l-ipc/
を参照してください。
Linux システムにおけるメッセージキューの概念と実装については、http://www.ibm.com/developerworks/cn/linux/l-ipc/part4/
を参照してください。
PHP の sysvmsg モジュールは、Linux システムでサポートされる System V IPC の System V メッセージ キュー関数ファミリーをカプセル化したものです。プロセス間通信には sysvmsg モジュールが提供する関数を使用する必要があります。まずサンプルコードを見てみましょう_1:
<?php $message_queue_key = ftok(__FILE__, 'a'); $message_queue = msg_get_queue($message_queue_key, 0666); var_dump($message_queue); $message_queue_status = msg_stat_queue($message_queue); print_r($message_queue_status); //向消息队列中写 msg_send($message_queue, 1, "Hello,World!"); $message_queue_status = msg_stat_queue($message_queue); print_r($message_queue_status); //从消息队列中读 msg_receive($message_queue, 0, $message_type, 1024, $message, true, MSG_IPC_NOWAIT); print_r($message."\r\n"); msg_remove_queue($message_queue); ?>?このコードを実行した結果は次のとおりです。
resource(4) of type (sysvmsg queue) Array ( [msg_perm.uid] => 1000 [msg_perm.gid] => 1000 [msg_perm.mode] => 438 [msg_stime] => 0 [msg_rtime] => 0 [msg_ctime] => 1279849495 [msg_qnum] => 0 [msg_qbytes] => 16384 [msg_lspid] => 0 [msg_lrpid] => 0 ) Array ( [msg_perm.uid] => 1000 [msg_perm.gid] => 1000 [msg_perm.mode] => 438 [msg_stime] => 1279849495 [msg_rtime] => 0 [msg_ctime] => 1279849495 [msg_qnum] => 1 [msg_qbytes] => 16384 [msg_lspid] => 2184 [msg_lrpid] => 0 ) Hello,World!?「Hello, World!」という文字列がメッセージ キューから正常に読み取られたことがわかります。
サンプルコードの主な関数は以下のとおりです:
ftok ( string $pathname , string $proj ) 手册上给出的解释是:Convert a pathname and a project identifier to a System V IPC key。这个函数返回的键值唯一对应linux系统中一个消息队列。在获得消息队列的引用之前都需要调用这个函数。 msg_get_queue ( int $key [, int $perms ] ) msg_get_queue()会根据传入的键值返回一个消息队列的引用。如果linux系统中没有消息队列与键值对应,msg_get_queue()将会创建一个新的消息队列。函数的第二个参数需要传入一个int值,作为新创建的消息队列的权限值,默认为0666。这个权限值与linux命令chmod中使用的数值是同一个意思,因为在linux系统中一切皆是文件。 msg_send ( resource $queue , int $msgtype , mixed $message [, bool $serialize [, bool $blocking [, int &$errorcode ]]] ) 顾名思义,该函数用来向消息队列中写数据。 msg_stat_queue ( resource $queue ) 这个函数会返回消息队列的元数据。消息队列元数据中的信息很完整,包括了消息队列中待读取的消息数、最后读写队列的进程ID等。示例代码在第8行调用该函数返回的数组中队列中待读取的消息数msg_qnum值为0。 msg_receive ( resource $queue , int $desiredmsgtype , int &$msgtype , int $maxsize , mixed &$message [, bool $unserialize [, int $flags [, int &$errorcode ]]] ) msg_receive用于读取消息队列中的数据。 msg_remove_queue ( resource $queue ) msg_remove_queue用于销毁一个队列。?サンプルコード_1は、PHPのメッセージキュー機能の応用例を示したものです。次のコードは、プロセス間通信シナリオを具体的に説明しています。
<?php $message_queue_key = ftok ( __FILE__, 'a' ); $message_queue = msg_get_queue ( $message_queue_key, 0666 ); $pids = array (); for($i = 0; $i < 5; $i ++) { //创建子进程 $pids [$i] = pcntl_fork (); if ($pids [$i]) { echo "No.$i child process was created, the pid is $pids[$i]\r\n"; } elseif ($pids [$i] == 0) { $pid = posix_getpid (); echo "process.$pid is writing now\r\n"; msg_send ( $message_queue, 1, "this is process.$pid's data\r\n" ); posix_kill ( $pid, SIGTERM ); } } do { msg_receive ( $message_queue, 0, $message_type, 1024, $message, true, MSG_IPC_NOWAIT ); echo $message; //需要判断队列是否为空,如果为空就退出 //break; } while ( true ) ?>実行結果は次のとおりです。
No.0 child process was created, the pid is 5249 No.1 child process was created, the pid is 5250 No.2 child process was created, the pid is 5251 No.3 child process was created, the pid is 5252 No.4 child process was created, the pid is 5253 process.5251 is writing now this is process.5251's data process.5253 is writing now process.5252 is writing now process.5250 is writing now this is process.5253's data this is process.5252's data this is process.5250's data process.5249 is writing now this is process.5249's data
レディス
http://www.neatstudio.com/show-976-1.shtml
?
PHP に付属する 3 つのメッセージ キュー関連関数
http://www.zhangguangda.com/?p=89?

PHPは、現代のWeb開発、特にコンテンツ管理とeコマースプラットフォームで依然として重要です。 1)PHPには、LaravelやSymfonyなどの豊富なエコシステムと強力なフレームワークサポートがあります。 2)パフォーマンスの最適化は、Opcacheとnginxを通じて達成できます。 3)PHP8.0は、パフォーマンスを改善するためにJITコンパイラを導入します。 4)クラウドネイティブアプリケーションは、DockerおよびKubernetesを介して展開され、柔軟性とスケーラビリティを向上させます。

PHPは、特に迅速な開発や動的なコンテンツの処理に適していますが、データサイエンスとエンタープライズレベルのアプリケーションには良くありません。 Pythonと比較して、PHPはWeb開発においてより多くの利点がありますが、データサイエンスの分野ではPythonほど良くありません。 Javaと比較して、PHPはエンタープライズレベルのアプリケーションでより悪化しますが、Web開発により柔軟性があります。 JavaScriptと比較して、PHPはバックエンド開発により簡潔ですが、フロントエンド開発のJavaScriptほど良くありません。

PHPとPythonにはそれぞれ独自の利点があり、さまざまなシナリオに適しています。 1.PHPはWeb開発に適しており、組み込みのWebサーバーとRich Functionライブラリを提供します。 2。Pythonは、簡潔な構文と強力な標準ライブラリを備えたデータサイエンスと機械学習に適しています。選択するときは、プロジェクトの要件に基づいて決定する必要があります。

PHPは、サーバー側で広く使用されているスクリプト言語で、特にWeb開発に適しています。 1.PHPは、HTMLを埋め込み、HTTP要求と応答を処理し、さまざまなデータベースをサポートできます。 2.PHPは、ダイナミックWebコンテンツ、プロセスフォームデータ、アクセスデータベースなどを生成するために使用され、強力なコミュニティサポートとオープンソースリソースを備えています。 3。PHPは解釈された言語であり、実行プロセスには語彙分析、文法分析、編集、実行が含まれます。 4.PHPは、ユーザー登録システムなどの高度なアプリケーションについてMySQLと組み合わせることができます。 5。PHPをデバッグするときは、error_reporting()やvar_dump()などの関数を使用できます。 6. PHPコードを最適化して、キャッシュメカニズムを使用し、データベースクエリを最適化し、組み込み関数を使用します。 7

PHPが多くのWebサイトよりも優先テクノロジースタックである理由には、その使いやすさ、強力なコミュニティサポート、広範な使用が含まれます。 1)初心者に適した学習と使用が簡単です。 2)巨大な開発者コミュニティと豊富なリソースを持っています。 3)WordPress、Drupal、その他のプラットフォームで広く使用されています。 4)Webサーバーとしっかりと統合して、開発の展開を簡素化します。

PHPは、特にWeb開発の分野で、最新のプログラミングで強力で広く使用されているツールのままです。 1)PHPは使いやすく、データベースとシームレスに統合されており、多くの開発者にとって最初の選択肢です。 2)動的コンテンツ生成とオブジェクト指向プログラミングをサポートし、Webサイトを迅速に作成および保守するのに適しています。 3)PHPのパフォーマンスは、データベースクエリをキャッシュおよび最適化することで改善でき、その広範なコミュニティと豊富なエコシステムにより、今日のテクノロジースタックでは依然として重要になります。

PHPでは、弱い参照クラスを通じて弱い参照が実装され、ガベージコレクターがオブジェクトの回収を妨げません。弱い参照は、キャッシュシステムやイベントリスナーなどのシナリオに適しています。オブジェクトの生存を保証することはできず、ごみ収集が遅れる可能性があることに注意する必要があります。

\ _ \ _ Invokeメソッドを使用すると、オブジェクトを関数のように呼び出すことができます。 1。オブジェクトを呼び出すことができるように\ _ \ _呼び出しメソッドを定義します。 2。$ obj(...)構文を使用すると、PHPは\ _ \ _ Invokeメソッドを実行します。 3。ロギングや計算機、コードの柔軟性の向上、読みやすさなどのシナリオに適しています。


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

WebStorm Mac版
便利なJavaScript開発ツール

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

DVWA
Damn Vulnerable Web App (DVWA) は、非常に脆弱な PHP/MySQL Web アプリケーションです。その主な目的は、セキュリティ専門家が法的環境でスキルとツールをテストするのに役立ち、Web 開発者が Web アプリケーションを保護するプロセスをより深く理解できるようにし、教師/生徒が教室環境で Web アプリケーションを教え/学習できるようにすることです。安全。 DVWA の目標は、シンプルでわかりやすいインターフェイスを通じて、さまざまな難易度で最も一般的な Web 脆弱性のいくつかを実践することです。このソフトウェアは、

AtomエディタMac版ダウンロード
最も人気のあるオープンソースエディター

ドリームウィーバー CS6
ビジュアル Web 開発ツール
