# PsyncLib PsyncLib (`net.nosial.psynclib`) is a PHP library that introduces parallel operations into PHP without the need for additional software such as a Message Queue or a Job Queue. Using Shared Memory & Forking, PsyncLib allows you to create parallel operations by calling several simple functions. ## Community This project and many others from Nosial are available on multiple publicly available and free git repositories at - [n64](https://git.n64.cc/nosial/psynclib) - [GitHub](https://github.com/nosial/psynclib) - [Codeberg](https://codeberg.org/nosial/psynclib) Issues & Pull Requests are frequently checked and to be referenced accordingly in commits and changes, Nosial remains dedicated to keep these repositories up to date when possible. For questions & discussions see the public Telegram community at [@NosialDiscussions](https://t.me/NosialDiscussions). We do encourage community support and discussions, please be respectful and follow the rules of the community. ## Table of contents * [PsyncLib](#psynclib) * [Community](#community) * [Table of contents](#table-of-contents) * [Versioning](#versioning) * [Installation](#installation) * [Usage](#usage) * [Classes](#classes) * [P](#p) * [Psync](#psync) * [public static function do(callable $callable, array $args = []): P](#public-static-function-docallable-callable-array-args---p) * [public static function isDone(P $p): bool](#public-static-function-isdonep-p-bool) * [public static function waitFor(P $p): mixed](#public-static-function-waitforp-p-mixed) * [private static function close(P $p): void](#private-static-function-closep-p-void) * [public static function wait(): array](#public-static-function-wait-array) * [public static function total(): int](#public-static-function-total-int) * [public static function running(): int](#public-static-function-running-int) * [public static function clean(): int](#public-static-function-clean-int) * [public static function getSharedMemorySize(): int](#public-static-function-getsharedmemorysize-int) * [public static function setSharedMemorySize(int $sharedMemorySize): void](#public-static-function-setsharedmemorysizeint-sharedmemorysize-void) * [public static function getSharedMemoryPermissions(): int](#public-static-function-getsharedmemorypermissions-int) * [public static function setSharedMemoryPermissions(int $sharedMemoryPermissions): void](#public-static-function-setsharedmemorypermissionsint-sharedmemorypermissions-void) * [License](#license) * [Contributing](#contributing) ## Versioning This library uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## Installation To install PsyncLib, you can use [ncc](https://git.n64.cc/nosial/ncc). (can also be found at [github](https://github.com/nosial/ncc)) Run the following command: ```bash ncc package install -p="nosial/libs.psync=latest@n64" ``` To add the package as a dependency to your project, add this entry under `build{} -> dependencies[]` in your `project.json` file: ```json { "name": "net.nosial.psynclib", "version": "latest", "source_type": "remote", "source": "nosial/libs.psync=latest@n64" } ``` The GitHub alternative name is `nosial/psynclib=latest@github` if you prefer to use GitHub as the source. ## Usage Psync is called under the namespace `\PsyncLib\Psync`. The following is an example of how to use Psync to calculate the value of Pi using the Monte Carlo method in parallel using 4 processes, this will display the value of Pi and the time taken to calculate it per process and finally the total time in total the script took to run. ```php $pi, 'time' => $end - $start]; }); } // Wait for all promises to resolve /** @var \PsyncLib\P $p */ foreach($promises as $p) { $result = \PsyncLib\Psync::waitFor($p); printf("Result: %f, Time: %f\n", $result['result'], $result['time']); } $parentEnd = microtime(true); printf("Parent Time: %f\n", $parentEnd - $parentStart); ``` Running this on a test machine, the output will be similar to the following: ``` Result: 3.141594, Time: 2.113697 Result: 3.141594, Time: 2.370120 Result: 3.141594, Time: 2.043246 Result: 3.141594, Time: 2.124459 Result: 3.141594, Time: 2.245291 Result: 3.141594, Time: 2.309868 Result: 3.141594, Time: 2.163053 Result: 3.141594, Time: 2.375659 Result: 3.141594, Time: 2.360129 Result: 3.141594, Time: 2.006329 Parent Time: 2.390980 ``` ## Classes Psync is a very lightweight library, it only has 2 classes: ### P The `P` class is a simple class that represents a promise, it is used to store the state of the promise, the object itself should be treated as a reference, it has contains no actual functionality. - `public function getUuid(): string` - Returns the UUID of the promise - `public function getPid(): int` - Returns the PID of the process - `public function getShm(): Shmop` - Returns the Shared Memory object - `public function __toString(): string` - Returns the UUID of the promise ### Psync The `Psync` class is the main class used to interact with the Psync Library #### public static function do(callable $callable, array $args = []): P This method creates a new promise by forking a process to execute the given callable function. The result of the callable is stored in shared memory and can be retrieved later. - **Parameters:** - `callable $callable`: The function to be executed in the forked process. - `array $args`: An optional array of arguments to pass to the callable. - **Returns:** - `P`: An instance of the `P` class representing the promise. - **Throws:** - `RuntimeException`: If the shared memory segment or the process fork fails. Usage Example: ```php $result) { echo "UUID: $uuid, Result: $result\n"; } ``` #### public static function total(): int This method calculates the total number of promises. - **Returns:** - `int`: The total number of promises. Usage Example: ```php