gearman 任务失败

tech2023-08-08  108

gearman 任务失败

How many times have you developed a web application that had some functionality which would benefit from running an external program or even forking a separate process? This is not something you generally like to do from your web app because you want to make it run as fast and efficient as possible, while keeping the site functional for end users. So how do we get a fast but full-featured application that can process more than the average app we're used to?

您开发了多少次具有某些功能的Web应用程序,而该功能将从运行外部程序甚至分叉一个单独的进程中受益? 通常,您不希望在Web应用程序中执行此操作,因为您希望使它尽可能快和高效地运行,同时保持站点对最终用户的正常运行。 那么,我们如何获得一个快速但功能齐全的应用程序,该应用程序可以处理比我们以往使用的普通应用程序更多的内容?

Years ago developers of a web site were facing a similar problem. Users were uploading too many pictures, all of which had to be processed into their respective accounts. This included resizing functionality such as creating thumbnails or reducing the raw image size to something more appropriate for the web site to display. These pictures mostly consisted of avatars, millions of them overloading the app. The team thought of a solution: have a server that spawns processes that do things such as processing pictures, outside of the web server application. This resulted in Gearman.

多年前,网站开发人员面临着类似的问题。 用户上载了太多图片,所有图片都必须处理到各自的帐户中。 这包括调整大小的功能,例如创建缩略图或将原始图像大小减小到更适合网站显示的大小。 这些图片主要由化身组成,其中数百万个使应用程序超载。 团队想到了一种解决方案:拥有一台服务器,该服务器在Web服务器应用程序外部生成用于执行诸如处理图片之类的过程的进程。 这导致了Gearman 。

什么是Gearman? (What is Gearman?)

Gearman provides a distributed application framework for work with multiple machines or processes. It allows applications to complete tasks in parallel, to load balance processing and to call functions between languages. The framework can be used in a variety of applications. Gearman is multi-threaded and is known to be able to carry out 50 thousand jobs per-second. Some of the well-known sites using the C version of Gearman are: – Digg: 45+ servers, 400 thousand jobs a day – Yahoo: 120+ servers, 12 million jobs a day

Gearman提供了一个分布式应用程序框架,可用于多个机器或进程。 它允许应用程序并行完成任务,负载平衡处理以及在语言之间调用函数。 该框架可用于多种应用程序。 Gearman是多线程的,并且已知每秒能够执行5万个作业。 一些使用C版Gearman的知名站点是:– Digg:45台服务器,每天40万个工作– Yahoo:120台服务器,每天1200万个工作

Gearman was originally written in Perl, but the job servers and client API were recently rewritten in C by Eric Day because he wanted better performance.

Gearman最初是用Perl编写的,但Eric Day最近用C重写了作业服务器和客户端API,因为他想要更好的性能。

The figure describes the type of setup you might use for image resizing. Traditionally, the image resizing would have been implemented completely within the web application. The user would upload an image, and within the HTTP request to serve the page, PHP would have to run the image conversion to perform the resizing. The page load would not be completed until the image resizing was complete. Now, with Gearman, the web app can request an image resizing by way of a Gearman client to the Gearman job server. Gearman allows you to separate some functionality from your web application letting other parts of your environment take care of it.

该图描述了可能用于图像大小调整的设置类型。 传统上,图像调整大小将完全在Web应用程序中实现。 用户将上传一个图像,并且在服务于该页面的HTTP请求中,PHP将必须运行图像转换以执行调整大小。 在图像调整大小之前,页面加载不会完成。 现在,借助Gearman,Web应用程序可以通过Gearman客户端向Gearman作业服务器请求调整图像大小。 Gearman允许您从Web应用程序中分离某些功能,让环境的其他部分来处理它。

安装和运行Gearman (Installing and Running Gearman)

Installing it is easy and straightforward if you just follow their comprehensive tutorial, assuming you have a high-level understanding of roles like a job server, clients, and workers; if you run into any problems while trying to install it, let us know in the comments below.

如果您只是按照他们的综合教程进行安装 ,并且假设您对工作服务器,客户端和工作人员等角色有高水平的了解,那么安装起来将非常容易和直接。 如果您在尝试安装时遇到任何问题,请在下面的评论中告诉我们。

Gearman和PHP (Gearman and PHP)

Using Gearman with PHP makes for an ideal easy-to-use combination. You have a Gearman client, which is usually your application and is the code that sends jobs to the Gearman job server. You also have the worker component, which uses the PHP Gearman worker library to register itself as handling a named job and then specifies the function name for the job. The PHP extension extends the procedural interface to provide a native object oriented interface as well.

将Gearman与PHP结合使用可实现理想的易用组合。 您有一个Gearman客户端,通常是您的应用程序,并且是将作业发送到Gearman作业服务器的代码。 您还具有工作程序组件,该组件使用PHP Gearman工作程序库将其自身注册为处理已命名的作业,然后为该作业指定函数名称。 PHP扩展扩展了过程接口,以提供本机面向对象的接口。

To install the Gearman extension into PHP, refer to your OS documentation. On Ubuntu and Linux Mint, for example, it's as easy as sudo apt-get install php5-gearman. On some systems, however, you might have to manually build it and include it into your PHP extensions folder (you can find out where it is by looking at your phpinfo()) and then include it in the php.ini file like so: extension="gearman.so".

要将Gearman扩展程序安装到PHP中,请参阅您的OS文档。 例如,在Ubuntu和Linux Mint上,它与sudo apt-get install php5-gearman一样容易。 但是,在某些系统上,您可能必须手动构建它并将其包含在PHP扩展文件夹中(您可以通过查看phpinfo()找出它的位置),然后将其包含在php.ini文件中,如下所示: extension="gearman.so" 。

To check if Gearman is successfully installed, see your phpinfo() again or just run a test method:

要检查Gearman是否已成功安装,请再次查看phpinfo()或仅运行测试方法:

<?php var_dump( gearman_version() ); ?>

Let's look at an example:

让我们看一个例子:

<?php $client= new GearmanClient(); $client->addServer(‘127.0.0.1’); $client->setCreatedCallback("create_change"); $client->setDataCallback("data_change"); $client->setStatusCallback("status_change"); $client->setCompleteCallback("complete_change"); $client->setFailCallback("fail_change"); $data_array =array('mydata'=>’task’); $task= $client->addTask("reverse", "mydata", $data_array); $task2= $client->addTaskLow("reverse", "task", NULL); echo "DONE\n"; function create_change($task) { echo "CREATED: " . $task->jobHandle() . "\n"; } function status_change($task) { echo "STATUS: " . $task->jobHandle() . " - " . $task->taskNumerator() . "/" . $task->taskDenominator() . "\n"; } function complete_change($task) { echo "COMPLETE: " . $task->jobHandle() . ", " . $task->data() . "\n"; } function fail_change($task) { echo "FAILED: " . $task->jobHandle() . "\n"; } function data_change($task) { echo "DATA: " . $task->data() . "\n"; } Function Client_error() { if (! $client->runTasks()) return $client->error() ; } ?>

In this client example, a GearmanClient object is instantiated. Next, the Gearman client API addServer() method is called to add a server to be used for the client connection. Multiple servers could be added if desired. An empty argument would default to localhost – it was explicitly specified here for the sake of clarity.

在此客户端示例中,实例化了GearmanClient对象。 接下来,调用Gearman客户端API addServer()方法以添加用于客户端连接的服务器。 如果需要,可以添加多个服务器。 空参数将默认为localhost为清楚起见,在此处明确指定了该参数。

Next, we set up some callbacks for various stages of the tasks – the concept of callbacks should be familiar to every intermediate developer.

接下来,我们为任务的各个阶段设置了一些回调–每个中级开发人员都应该熟悉回调的概念。

addTask adds a task to be run in parallel with other tasks. addTaskLow adds a low priority background task to be run in parallel with other tasks. To perform the work, we call GearmanClient::runTasks(). Note that enough workers need to be available for the tasks to run in parallel. Tasks with a lower priority will be chosen from the queue after those of higher priority.

addTask添加一个要与其他任务并行运行的任务。 addTaskLow添加一个低优先级的后台任务,使其与其他任务并行运行。 为了完成这项工作,我们调用GearmanClient::runTasks() 。 请注意,需要有足够的工人来使任务并行运行。 优先级较低的任务将在优先级较高的任务之后从队列中选择。

结论 (Conclusion)

In this short introduction into Gearman, you learned about multitasking with PHP applications. You can now implement certain functionality external to your web application and achieve better performance while leaving little to none of the rest of your system idle.

在Gearman的简短介绍中,您了解了PHP应用程序的多任务处理。 现在,您可以在Web应用程序外部实现某些功能,并获得更好的性能,同时使系统其余部分几乎保持空闲。

In a future article, we'll cover a detailed real world use case of Gearman with a working demo. For now, please don't hesitate to leave a comment below if you'd like anything covered in more detail.

在以后的文章中,我们将通过一个有效的演示来介绍Gearman的详细实际使用案例。 现在,如果您想更详细地介绍任何内容,请不要在下面留下评论。

翻译自: https://www.sitepoint.com/introduction-gearman-multi-tasking-php/

gearman 任务失败

最新回复(0)