Queue background process in Php with Gearman / ~#root -i Queue background process in Php with Gearman / ~#root -i Queue background process in Php with Gearman / ~#root -i Queue background process in Php with Gearman / ~#root -i Queue background process in Php with Gearman / ~#root -i Queue background process in Php with Gearman / ~#root -i Queue background process in Php with Gearman / ~#root -i Queue background process in Php with Gearman / ~#root -i
  • RU
  • UA
  • EN
  • Create an online store
  • Documentation
  • Blog
    • Unix ОS
    • Php
    • MySQL
    • JavaScript
    • Package Managers
    • Docker
    • Seo
  • Auxiliary services
    • Short Links
    • Exchange views YouTube
  • Sign in
  • Create Account
  • Home
  • Php
  • Queue background process in Php with Gearman

Queue background process in Php with Gearman

About installing Gearman Job Server - you can read here.

If everything worked out according to the Gearman installation instructions, then you can continue.

To begin with, for proper operation, you will need to create a certain worker (worker) with which the client (client) in Php will communicate, queuing its tasks in the background or waiting for a response.

I will create the simplest example on two files, which will allow you to understand the logic of Gearman. Our polygon will consist of two files:

Gearman+php пример

server.php - the file in which we will create a worker that will process all requests. In this example, it will be available via a local link: http://test1.loc/server.php

client.php - the file in which we will set tasks for the created worker. This will be our client, who will need to set background tasks. In this example, it will be available via a local link: http://test1.loc/client.php

1. Let's look at the server.php worker's pending task part :

<?php

$gmw = new GearmanWorker();

/**
 * Pokey what to look at initiating addServer() for shutting down
 * This method is required for rendering on which machine the worker works
 */
$gmw->addServer();

/**
 * log_client - on the side of the client to work with alice
 * work_log - function, on how to throwtime a worker, on a
 * on alice log_client will come.
 */
$gmw->addFunction('log_client', 'work_log');

/**
  *Specially broken task
 * If you simply call $gmw->work(), the task will end immediately
 * after the end of the task. To do this only once.
 */
while($gmw->work());

/**
 * Write logs to the file
 * @param JSON $param - Accepts only a row, which is passed to json as needed
&nbsp ;*/
function work_log($param) {
    /*Get data sent by the client*/
    $data = $param->workload();
    
    /**
     * Piece by piece robimo navantazhennya, schob bula іmitatsіya great ochіkuvannya.
     * Here we care that the client script is not checkable for 100 seconds,
     * but set the task and forget about it.
     */
    for ($i = 0; $i < 100; $i++) {
        sleep(1);
        file_put_contents('log.txt', $data, FILE_APPEND);
    }
    
}

2. Let's look at the client's backlog, which is the command to the worker client.php:

<?php

$client = new GearmanClient();
/**
  A worker is checking on us. While everything is being fixed after the lock
 */
$client->addServer();

/**
 * doBackground() - we can do the task
 * log_client - alice to the work_log() function from server.php to the function work_log();
 */
$client->doBackground('log_client', "Test LOG. \r\n");    
   

You have been shown the simplest working prototype of the task put on the Php side in the background mode.

3. How does it work?

Scheme of the operation of the Gearman service is idle. server.php contains the digest itself. For the time being, it will run in the browser, but then send to http://test1.loc/server.php. Forces the browser to become permanently cleared through rows of while($gmw->work());

To run $gmw->work(); not in a cycle, then after the launch of the client part, the script runs 1 time and ends.

Last weekend we will run client.php against http://test1.loc/client.php. Alternatively, the work_log() function of server.php will run and write the word that is sent by the client to the log.txt file for 100 seconds, 100 times.

Yakby didn't work without gearman, but the script took 100 seconds to finish. Ale zavdyaka background zadanny doBackground(), the client does not mind the time and the script ends without a check-in.

19 May 18
1300
0

Comments

Name
E-mail
Rating
Review

Other articles from the category

25 April 2022

Create and download CSV in PHP

Consider the possibility of quickly creating a CSV file with automatic file download. Consider the formation, separators and header for the ability to download the file.

19 June 2019

ImageCMS Templates 4.10-4.12.1

Latest templates from ImageCMS 4.12, authorization is required to download.

26 January 2019

Long-term storage of the basket in the online store

Consider options for popular options for storing goods in a shopping cart in an online store. Let's outline the pros and cons of such storage. Consider options for long-term storage of the basket.

17 January 2019

License key for ImageCMS 4.9-4.12.1 Pro and Pre

Creating a key for imageCMS 4.9-4.12.1 Pro and Pre. You must be logged in to receive it.

07 December 2018

$_SERVER[DOCUMENT_ROOT] in CLI or Cron

When running the script from the console, there is no DOCUMENT_ROOT in $_SERVER. Let's try to get around this in our own ways, catch the file directory.

31 October 2018

Analysis of loaded parts of Php code with xhprof

After developing any tool in PHP, the question becomes how resourceful the created code is and what elements it still affects. Let's take a look at xhprof installation, configuration, and how to work with xhprof.

18 July 2018

Grouping conditions in an ORM Propel query (condition, combine)

Let's analyze the possibility of combining conditions in a query by groups in Propel. Consider an example of filtering by fields using ->condition() and ->combine().

14 June 2018

Authorization 1C on the site PHP_AUTH_USER on CentOS apache

Faced the problem of authorization 1s on the site. For some reason, the server did not accept the PHP_AUTH_USER and PHP_AUTH_PW parameters from it. Let's take a closer look at the solution and bypassing these parameters in $_SERVER .

Categories

  • Unix OS
  • Php
  • MySQL
  • JavaScript
  • Package Managers
  • Docker
  • Seo

Latest comments

Добрый день, Сергей. Я на более новых версиях блют...
root-i
23.02.23
Пробовал на transmart колонке. Ничего из перечисле...
Сергей
20.02.23
HenryMit, может быть
root-i
07.02.23
Неофрейдизм — это… Определение, принципы, представ...
HenryMit
07.02.23

I share information in which I needed help and spent a lot of time figuring it out. If the information helped at least one person, then this site was not created in vain.

Thank you for the continuation of the site:
Contacts

Telegram Viber Mail

Search for articles

  • Sign in
  • Create Account

Powered by chmod -R