PHP
मल्टी थ्रेडिंग एक्सटेंशन
खोज…
टिप्पणियों
साथ
pthreadsv3pthreadsकेवल का उपयोग करते समय लोड किया जा सकताcliSAPI, इस प्रकार यह रखने के लिए एक अच्छा अभ्यास हैextension=pthreads.soनिर्देश मेंphp-cli.iniही, आप PHP7 और pthreads v3 का उपयोग कर रहे हैं।
यदि आप Windows पर Wamp का उपयोग कर रहे हैं, तो आपको php.ini में एक्सटेंशन को कॉन्फ़िगर करना होगा :
Php \ php.ini खोलें और जोड़ें:
extension=php_pthreads.dll
लिनक्स उपयोगकर्ताओं के संबंध में, आपको .dll को .so से प्रतिस्थापित करना होगा:
extension=pthreads.so
आप इसे सीधे php.ini (अपने कस्टम पथ के साथ /etc/php.ini ) में जोड़ने के लिए इस कमांड को निष्पादित कर सकते हैं
echo "extension=pthreads.so" >> /etc/php.ini
शुरू करना
मल्टी-थ्रेडिंग के साथ शुरू करने के लिए, आपको php के लिए pthreads-ext आवश्यकता होगी, जिसे द्वारा स्थापित किया जा सकता है
$ pecl install pthreads
और php.ini में प्रविष्टि जोड़ना।
एक सरल उदाहरण:
<?php
// NOTE: Code uses PHP7 semantics.
class MyThread extends Thread {
/**
* @var string
* Variable to contain the message to be displayed.
*/
private $message;
public function __construct(string $message) {
// Set the message value for this particular instance.
$this->message = $message;
}
// The operations performed in this function is executed in the other thread.
public function run() {
echo $this->message;
}
}
// Instantiate MyThread
$myThread = new MyThread("Hello from an another thread!");
// Start the thread. Also it is always a good practice to join the thread explicitly.
// Thread::start() is used to initiate the thread,
$myThread->start();
// and Thread::join() causes the context to wait for the thread to finish executing
$myThread->join();
पूल और वर्कर्स का उपयोग करना
पूलिंग वर्कर कार्यक्षमता का एक उच्च स्तर प्रदान करता है, जिसमें pthreads द्वारा आवश्यक तरीके से संदर्भों का प्रबंधन भी शामिल है। प्रेषक: http://php.net/manual/en/class.pool.php
पूल और श्रमिक बहु-थ्रेडेड बनाने के लिए उच्च स्तर का नियंत्रण और आसानी प्रदान करते हैं
<?php
// This is the *Work* which would be ran by the worker.
// The work which you'd want to do in your worker.
// This class needs to extend the \Threaded or \Collectable or \Thread class.
class AwesomeWork extends Thread {
private $workName;
/**
* @param string $workName
* The work name wich would be given to every work.
*/
public function __construct(string $workName) {
// The block of code in the constructor of your work,
// would be executed when a work is submitted to your pool.
$this->workName = $workName;
printf("A new work was submitted with the name: %s\n", $workName);
}
public function run() {
// This block of code in, the method, run
// would be called by your worker.
// All the code in this method will be executed in another thread.
$workName = $this->workName;
printf("Work named %s starting...\n", $workName);
printf("New random number: %d\n", mt_rand());
}
}
// Create an empty worker for the sake of simplicity.
class AwesomeWorker extends Worker {
public function run() {
// You can put some code in here, which would be executed
// before the Work's are started (the block of code in the `run` method of your Work)
// by the Worker.
/* ... */
}
}
// Create a new Pool Instance.
// The ctor of \Pool accepts two parameters.
// First: The maximum number of workers your pool can create.
// Second: The name of worker class.
$pool = new \Pool(1, \AwesomeWorker::class);
// You need to submit your jobs, rather the instance of
// the objects (works) which extends the \Threaded class.
$pool->submit(new \AwesomeWork("DeadlyWork"));
$pool->submit(new \AwesomeWork("FatalWork"));
// We need to explicitly shutdown the pool, otherwise,
// unexpected things may happen.
// See: http://stackoverflow.com/a/23600861/23602185
$pool->shutdown();