|
| 1 | +[<-- Return to index](../README.md) |
| 2 | +# Multithreading |
| 3 | + |
| 4 | +### How is multitasking accomplished? |
| 5 | +#### PHP |
| 6 | +By extending the [`Thread`](https://secure.php.net/manual/en/class.thread.php) class in the [pthreads API](https://secure.php.net/manual/en/intro.pthreads.php). It should be noted that by most defaults, PHP will be compiled as thread-safe and without pthreads. It requires a non-negligible amount of work to both recompile or obtain a version of PHP will threading enabled, and the pthreads API installed. |
| 7 | +#### Python |
| 8 | +By subclassing the [`Thread`](https://docs.python.org/3.6/library/threading.html#thread-objects) class from the [threading library](https://docs.python.org/3.6/library/threading.html#), which is native to Python. |
| 9 | + |
| 10 | +### How do threads or thread-like abilities work? |
| 11 | +#### PHP |
| 12 | +Parallel threading in PHP is less elegant than in Python, but ultimately just as functional. A parallel threading system in a basic form can look something like this: |
| 13 | +```php |
| 14 | +<?php |
| 15 | + class myThread extends Thread { |
| 16 | + private $id; |
| 17 | + public function __construct($id) { |
| 18 | + $this->id = $id; |
| 19 | + } |
| 20 | + public function run() { |
| 21 | + $sleepTime = mt_rand(1, 5); |
| 22 | + print "Thread with id ".$id."sleeping for ".$sleepTime." seconds"; |
| 23 | + sleep($sleepTime); |
| 24 | + print "Thread with id ".$id." is ending"; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + $threads = array(); |
| 29 | + foreach(range(0, 10) as $i) { |
| 30 | + $threads[] = new myThread($i); |
| 31 | + } |
| 32 | + |
| 33 | + foreach($threads as $t) { |
| 34 | + $t->run(); |
| 35 | + } |
| 36 | +?> |
| 37 | +``` |
| 38 | + |
| 39 | +#### Python |
| 40 | +Python's threading capabilities are dense and very well documented. A basic parallel threading system can look something like this: |
| 41 | +```python |
| 42 | +import time |
| 43 | +import random |
| 44 | +from threading import Thread |
| 45 | + |
| 46 | +def myThreadedMethod(id): |
| 47 | + sleepTime = random.randint(1, 5) |
| 48 | + print("Thread with id %d is sleeping for %d seconds"%(id, sleepTime)) |
| 49 | + time.sleep(sleepTime) |
| 50 | + print("Thread with id %d is ending"%(id)) |
| 51 | + |
| 52 | +for i in range(0, 10): |
| 53 | + t = Thread(target=myThreadedMethod, args=(i,)) |
| 54 | + t.start() |
| 55 | +``` |
| 56 | +Which will output: |
| 57 | +``` |
| 58 | +Thread with id 0 is sleeping for 2 seconds |
| 59 | +Thread with id 1 is sleeping for 3 seconds |
| 60 | +Thread with id 2 is sleeping for 5 seconds |
| 61 | +Thread with id 3 is sleeping for 5 seconds |
| 62 | +Thread with id 4 is sleeping for 3 seconds |
| 63 | +Thread with id 5 is sleeping for 2 seconds |
| 64 | +Thread with id 6 is sleeping for 5 seconds |
| 65 | +Thread with id 7 is sleeping for 2 seconds |
| 66 | +Thread with id 8 is sleeping for 2 seconds |
| 67 | +Thread with id 9 is sleeping for 1 seconds |
| 68 | +
|
| 69 | +Thread with id 9 is ending |
| 70 | +Thread with id 0 is ending |
| 71 | +Thread with id 5 is ending |
| 72 | +Thread with id 8 is ending |
| 73 | +Thread with id 7 is ending |
| 74 | +Thread with id 1 is ending |
| 75 | +Thread with id 4 is ending |
| 76 | +Thread with id 2 is ending |
| 77 | +Thread with id 3 is ending |
| 78 | +Thread with id 6 is ending |
| 79 | +``` |
| 80 | +Note how the threads end at the end of their execution, not necessarily in the same order they were spawned. This demonstrates how all threads were running in parallel. |
0 commit comments