无法使Beanstalkd队列适用于PHP [英] Unable to get Beanstalkd Queue to work for PHP

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

问题描述

我让Ubuntu运行XAMPP(灯栈:Linux,Apache,MySQL,PHP,Pear ).我想一起使用PHP和Beanstalkd来创建一个简单的队列,当用户进入 page1.php 时,会将 JOB 发送到 QUEUE 以捕获工作者. 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的东西,我读到它是一个包装纸 对于beantalkd,很多人都在网上使用它,但是我不确定这是否是必需的.任何人都可以通过我需要调用的函数或需要在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以安装作曲家,这是推荐用于安装pheanstalk的程序.
  3. 创建一个composer.json文件,该文件将使作曲家知道要下载的库以及该库的哪个版本.例如:

  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": {
    "pda\pheanstalk": "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天全站免登陆