承接 flytachi/winter-thread 相关项目开发

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

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

flytachi/winter-thread

最新稳定版本:v1.0.3

Composer 安装命令:

composer require flytachi/winter-thread

包简介

Winter framework Thread component

README 文档

README

Latest Version on Packagist PHP Version Require Software License

Winter Thread provides a clean, object-oriented API for running and controlling background processes in PHP, simulating a traditional threading model for parallel and long-running tasks.

It abstracts away the complexities of proc_open and posix signals into a powerful and easy-to-use interface.

Key Features

  • 🚀 Fluent, Object-Oriented API: Manage background processes as objects.
  • ⏯️ Full Process Control: start(), join(), pause(), resume(), terminate(), and kill().
  • 🏷️ Advanced Process Naming: Identify your processes easily with namespaces, names, and tags.
  • 🔒 Secure by Default: Optional signed serialization for closures via opis/closure.
  • 🧩 Extensible: Easily override the runner script for deep framework integration.
  • Java-like API: Familiar method names like isAlive() and join() for an easy learning curve.

Requirements

  • PHP >= 8.1
  • ext-pcntl
  • ext-posix
  • (Optional) opis/closure for serializing closures.

Installation

composer require flytachi/winter-thread

Quick Start

Here's how easy it is to run a long-running task in the background without blocking your main script.

<?php

require 'vendor/autoload.php';

use Flytachi\Winter\Thread\Runnable;
use Flytachi\Winter\Thread\Thread;

// 1. Define your task by implementing the Runnable interface.
//    The logic inside run() will be executed in a separate process.
class VideoProcessingTask implements Runnable {
    private string $videoFile;

    public function __construct(string $videoFile) {
        $this->videoFile = $videoFile;
    }

    public function run(): void {
        echo "Child Process (PID: " . getmypid() . "): Starting processing for {$this->videoFile}...\n";
        sleep(10); // Simulate a long-running encoding job
        echo "Child Process (PID: " . getmypid() . "): Finished processing {$this->videoFile}.\n";
    }
}

echo "Main Script (PID: " . getmypid() . "): Starting application.\n";

// 2. Create a new Thread instance with your task.
//    You can also provide metadata for process identification.
$thread = new Thread(
    new VideoProcessingTask('movie.mp4'),
    'ETL',          // Namespace
    'ProcessVideo', // Name
    'job-42'        // Tag
);

// 3. Start the thread. This immediately returns the child process PID.
$pid = $thread->start();

echo "Main Script: Video processing started in background (PID: $pid).\n";

// The main script can continue doing other work here...
echo "Main Script: Doing other work while video is processing...\n";
sleep(2);

// 4. (Optional) Wait for the thread to finish and get its exit code.
//    This will block the main script until the child process completes.
echo "Main Script: Waiting for the task to complete...\n";
$exitCode = $thread->join();
echo "Main Script: Task completed with exit code: $exitCode.\n";

Documentation

For detailed information on advanced features, process control, configuration, and examples, please see our full documentation in the /docs directory. (You can create this directory later)

Contributing

Contributions are welcome! Please feel free to submit a pull request or create an issue for bugs, questions, or feature requests.

License

This library is open-source software licensed under the MIT license.

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-18