无法让 Beanstalkd 队列为 PHP 工作 [英] Unable to get Beanstalkd Queue to work for PHP

查看:21
本文介绍了无法让 Beanstalkd 队列为 PHP 工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有运行 XAMPP 的 Ubuntu(灯栈:Linux、Apache、MySQL、PHP、Pear).我想用 PHP 和 Beanstalkd 一起做一个简单的队列,当用户继续 page1.php 时,一个 JOB 被发送到 QUEUE 用于捕获 WORKER.JOB 将是 WORKER 将执行的 SQL 语句:

I have Ubuntu running XAMPP (the lamp stack: Linux, Apache, MySQL, PHP, Pear). I would like to use PHP and Beanstalkd together to make a simple queue that when a user goes on page1.php, a JOB is sent to the QUEUE for a WORKER to capture. The JOB would be an SQL statement that the WORKER would then execute:

到目前为止我所做的是:

  1. 已安装 Beanstalkd:sudo apt-get install beanstalkd

开发的 php 代码和必须在 page1.php 中完成的工作".工作是将 sql 语句 $sql 发送到队列以供工作人员执行(在未来的版本中这项工作将更加复杂,因此队列系统将更加重要).:

Developed php code and the "job" that has to be done in page1.php. The job would be to send the sql statement $sql to the queue for the workers to execute (in future versions the job will be much more complex hence the queue system will be even more important).:

page1.php:

if (isset($_SESSION['authenticated']))
{
    //if the user is logged in, send an sql statement to the queue
    $user_id = $_SESSION['id'];
    $sql = "UPDATE user_table SET count = count + 1 WHERE id = {$user_id}";

    //... missing code that would send the statement
}

?>

  1. 制定了工人必须执行的操作.
  1. Developed the actions that have to be done by the WORKER.

工人:

<?php

    $stmt = $conn->query($sql);//simple update

?>

问题/问题:

问题是我不知道要调用什么函数来创建一个工作者,不知道要调用什么函数来发送队列.我在网上搜索了各种例子,但没有完整的解释,解释也很模糊.我已经看到存在一种叫做 pheanstalkd 的东西,我读到它是一个包装器对于 beanstalkd,很多人都在网上使用它,但我不确定这是否是必需的.任何人都可以通过我需要调用哪些函数或我需要在 linux 终端中执行哪些代码来引导我进入正确的方向,以使这个示例正常工作吗?非常感谢所有反馈,这将帮助我这周不再掉头发.

The problem is I don't know what functions to call that create a worker, what function to call to send the queue. I have searched online various examples, but there are no complete ones and with very vague explanations. I have seen that something called pheanstalkd exists, which I read was a wrapper for beanstalkd and a lot of people are using it online, but I'm not sure if this is a requirement or not. Can anyone guide me into the right direction with what functions I need to call or what codes I need to execute in the linux terminal just to get this one example working? All feedback is very much appreciated and would help me not loose any more hair this week.

推荐答案

解决方案:

经过更多研究,我已经设法让它工作了!为了达到这一点,缺少了相当多的数量.过程如下:

After some more research, I have managed to get it to work! A decent amount was missing to get to that point. The process was the following:

  1. 在linux终端执行sudo apt-get install beanstalkd安装beantalkd.
  2. 执行sudo apt install composer安装composer,推荐使用该程序安装pheanstalk.
  3. 创建一个 composer.json 文件,让 composer 知道要下载哪个库以及该库的版本.例如:

  1. Execute sudo apt-get install beanstalkd in the linux terminal to install beanstalkd.
  2. Execute sudo apt install composer to install composer, which is the program recommended to be used to install pheanstalk.
  3. Create a composer.json file that will let composer know what library to download and what version of said library. For instance:

{
  "require": {
    "pdapheanstalk": "2.1.1"
  }
}

  • 在 linux 终端中执行 composer install.这必须在 composer.json 文件所在的文件夹中完成.

  • Execute composer install in the linux terminal. This has to be done in the same folder as the composer.json file.

    包括启动 Pheanstalk 类的必要代码,并按照文档说明使用它.就是这样!示例代码如下:

    Include the necessary code that will initiate the Pheanstalk class, and use it as documented. And that is it! Sample code would be as follows:

    <?php
    
    require_once('vendor/autoload.php');//require the autoload file provided by
                                        //composer
    
    //Initiate an instance of the Pheanstalk class
    $pheanstalk = new Pheanstalk_Pheanstalk('127.0.0.1');
    
    //adding a job to queue/tube testtube:
    $pheanstalk->useTube('testtube')->put('message');
    
    //obtaining the job by a worker:
    $job = $pheanstalk->watch('testtube')->ignore('default')->reserve();
    
    echo $job->getData;//outputting the message
    
    $pheanstalk->delete($job);//deleting the job from the queue.
    
    ?>
    

  • 这篇关于无法让 Beanstalkd 队列为 PHP 工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆