承接 mjohann/zynq 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mjohann/zynq

Composer 安装命令:

composer require mjohann/zynq

包简介

Zynq is a PHP library that brings JavaScript-like asynchronous features to PHP, including support for timers, promises, async/await behavior, and multi-threaded execution via RPC.

README 文档

README

Zynq is a PHP library that brings JavaScript-like asynchronous features to PHP, including support for timers, promises, async/await behavior, and multi-threaded execution via RPC.

📦 Installation

Install via Packagist/Composer:

composer require mjohann/zynq

⚙️ Requirements

  • PHP 8.0 or higher

🚀 Features

Zynq is a PHP library that brings JavaScript-like asynchronous features to PHP, enabling a smoother and more efficient programming experience. The currently supported features include:

⏱️ Timers

  • setTimeout(callback, delay): Executes a function after a specified delay.
  • setInterval(callback, interval): Repeatedly executes a function at fixed intervals.
  • clearTimeout(timerId): Cancels a timer previously created with setTimeout.
  • clearInterval(timerId): Cancels a timer previously created with setInterval.

🔄 Promises & Flow Control

  • Promise: Native-like implementation for handling asynchronous operations.
  • then(onFulfilled): Defines what to do when a promise is fulfilled.
  • catch(onRejected): Handles errors when a promise is rejected.
  • finally(onFinally): Executes code after a promise is settled, regardless of the outcome.
  • await: Waits for a promise to resolve before continuing execution.
  • async: Declares an asynchronous function that returns a promise.

🧠 Asynchronous Execution & RPC

  • RPC (Remote Procedure Call): Allows executing functions in separate processes using a lightweight RPC mechanism.
  • Multi-process Execution: Simulates multithreading using child processes to improve performance in asynchronous and intensive tasks.

🧪 Usage Examples

📺 Full usage guide (previous version) on YouTube

⏲️ Timers

Zynq provides setTimeout, setInterval, clearTimeout, and clearInterval, mirroring their JavaScript counterparts.

Key Concepts

  • A callback is a function passed as a parameter to another function. In PHP, callbacks are typically of type callable.
  • PHP's Closure class is used to represent anonymous and arrow functions.
  • Named functions can also be used as callbacks.

Functions

  • setInterval(callback, milliseconds) — Repeatedly executes the given callback every X milliseconds. Returns a unique ID (UID) for managing the interval.
  • setTimeout(callback, milliseconds) — Executes the callback once after X milliseconds. Returns a UID.
  • clearInterval(UID) — Stops the scheduled setInterval by UID. Returns true if successful, false otherwise.
  • clearTimeout(UID) — Stops the scheduled setTimeout by UID. Returns true if successful, false otherwise.

Example

<?php

declare(ticks=1);

use function MJohann\Packlib\Functions\{clearInterval, setInterval, setTimeout, workWait};

require_once "vendor/autoload.php";

echo "Start", PHP_EOL;

$counter = 1;

$intervalId = setInterval(function () use (&$counter) {
    echo "Counter: {$counter}", PHP_EOL;
    $counter++;
}, 100);

setTimeout(function () {
    echo "Halfway through", PHP_EOL;
}, 1000);

setTimeout(function () use ($intervalId) {
    echo "Stopping the counter", PHP_EOL;
    clearInterval($intervalId);
}, 2000);

echo "Processing...", PHP_EOL;

workWait(function () { usleep(1); });

ℹ️ Zinq enables staggered execution of the main thread through the features provided by Timers. However, blocking actions (such as long-running or synchronous code) can interrupt this staggered execution. True parallelism is only achieved when using the RPC::send feature or its alias async.

🔄 Promises & Flow Control

Zynq includes a Promise class inspired by JavaScript's native Promise implementation.

API

  • then(callback) — Called when the promise is resolved. The callback receives the resolved value.
  • catch(callback) — Called when the promise is rejected. The callback receives the rejection reason.
  • finally(callback) — Called when the promise is either resolved or rejected.

Example

<?php

declare(ticks=1);

use MJohann\Packlib\Promise;
use function MJohann\Packlib\Functions\{setTimeout, workWait};

require_once "vendor/autoload.php";

echo "Start", PHP_EOL;

$promise = new Promise(function ($resolve, $reject) {
    $callback = rand(0, 1) ? $resolve : $reject;

    setTimeout(function () use ($callback) {
        $callback("message");
    }, 1000);
});

$promise
    ->then(function ($result) {
        echo "then: ", $result, PHP_EOL;
    })
    ->catch(function ($error) {
        echo "catch: ", $error, PHP_EOL;
    })
    ->finally(function () {
        echo "finally", PHP_EOL;
    });

echo "Processing loop...", PHP_EOL;

for ($i = 0; $i < 10; $i++) {
    echo "Counter: ", $i, PHP_EOL;
    usleep(200000); // 200ms
}

workWait(function () { usleep(1); });

📡 RPC (Remote Procedure Call)

Zynq provides a lightweight RPC system that allows you to register and call functions asynchronously in isolated processes. This is useful for offloading heavy computations or running code in parallel without blocking the main execution thread.

🔧 How It Works

  • Function Registration: You can register named functions to be called remotely.
  • Asynchronous Execution: Registered functions are invoked asynchronously and return a Promise.
  • Process Isolation: Each call runs in a separate process, ensuring non-blocking execution and memory isolation.

🧪 Example Usage

<?php

declare(ticks=1);

use MJohann\Packlib\RPC;
use function MJohann\Packlib\Functions\{await, workWait};

require_once "vendor/autoload.php";

// Initialize the WebThread system with the RPC endpoint and a secret key
RPC::init("http://localhost:8080/rpc.php", "secret");

// Send a remote function
$promise = RPC::send(
    function () {
        sleep(2);
        return 5 + 3;
    }
);

// Call the registered function asynchronously
$result = await($promise);
echo "Sum: ", $result, PHP_EOL; // Sum: 8

workWait(function () { usleep(1); });

📂 More examples available in the example/ folder.

📁 Project Structure

zynq/
├── src/
│   ├── functions.php
│   ├── Promise.php
│   ├── Timers.php
│   └── RPC.php
├── example/
│   ├── promise.php
│   ├── timers.php
│   ├── rpc_async_await.php
│   ├── rpc.php
│   └── run.bat
├── composer.json
├── .gitignore
├── LICENSE
└── README.md

📄 License

This project is licensed under the MIT License.

👨‍💻 Author

Developed by Matheus Johann Araújo – Pernambuco, Brazil.

mjohann/zynq 适用场景与选型建议

mjohann/zynq 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 9, 最近一次更新时间为 2025 年 05 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 mjohann/zynq 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mjohann/zynq 我们能提供哪些服务?
定制开发 / 二次开发

基于 mjohann/zynq 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 31
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 2
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-01