


An explanation of how python's distributed task huey implements asynchronous tasks_PHP tutorial
Explanation on how python's distributed task huey implements asynchronous tasks
In this article we will share a python lightweight task queue program, which can enable python's distributed task huey to be implemented Asynchronous tasks, interested friends can take a look.
A lightweight task queue. Its functions and related brokers are not as powerful as celery. It focuses on being lightweight, and the code is relatively simple to read.
Introduction to huey: (Lighter than celery, easier to use than mrq and rq!)
a lightweight alternative.
Written in python
No deps outside stdlib, except redis (or roll your own backend)
Support for django
supports:
Multi-threaded task execution
Scheduled execution at a given time
Periodic execution, like a crontab
Retrying tasks that fail
Task result storage
Installation:
The code is as follows | |||||||||
Installing
|
The code is as follows | |
from huey import RedisHuey, crontab huey = RedisHuey('my-app', host='redis.myapp.com') @huey.task() def add_numbers(a, b): Return a + b @huey.periodic_task(crontab(minute='0', hour='3')) def nightly_backup(): sync_all_data() |
When juey is a woker, some cli parameters.
Commonly used ones are:
-l About the execution of log files.
-WORKERS, -W has a large value, it must be the ability to increase tasks
-p --periodic When starting huey worker, it will find tasks that require crontab from tasks.py, and will send out several threads to handle these tasks.
-n does not start the pre-periodic execution in crontab. Weekly tasks will only be executed when you trigger it.
--threads You know what it means.
1
The code is as follows | |||||||||
# Original text:
The following table lists the options available for the consumer as well as their default values.
-l, --logfile
|
The code is as follows | |
# config.py from huey import Huey from huey.backends.redis_backend import RedisBlockingQueue queue = RedisBlockingQueue('test-queue', host='localhost', port=6379) huey = Huey(queue) |
Then it’s about tasks, that is, who you want to be in the task queue circle. Like celey, rq, and mrq, they are all represented by tasks.py.
The code is as follows | |||||
from config import huey # import the huey we instantiated in config.py
|
代码如下 | |
main.py from config import huey # import our "huey" object from tasks import count_beans # import our task if __name__ == '__main__': beans = raw_input('How many beans? ') count_beans(int(beans)) print 'Enqueued job to count %s beans' % beans Ensure you have Redis running locally Ensure you have installed huey Start the consumer: huey_consumer.py main.huey (notice this is “main.huey” and not “config.huey”). Run the main program: python main.py |
Here’s another one that actually goes into execution. main.py is equivalent to the producer, and tasks.py is equivalent to the consumer relationship. main.py is responsible for feeding data.
The code is as follows | |||||||||
main.py from config import huey # import our "huey" object from tasks import count_beans # import our task if __name__ == '__main__':
Count_beans(int(beans))
|
The code is as follows | |
from huey import Huey from huey.backends.redis_backend import RedisBlockingQueue from huey.backends.redis_backend import RedisDataStore # ADD THIS LINE queue = RedisBlockingQueue('test-queue', host='localhost', port=6379) result_store = RedisDataStore('results', host='localhost', port=6379) # ADDED huey = Huey(queue, result_store=result_store) # ADDED result store |
The code is as follows | |
>>> from main import count_beans
>>> res = count_beans(100)
>>> res # what is "res" ?
|
huey also supports celey's delayed execution and crontab functions. These functions are very important, and the priority can be customized or there is no need to rely on Linux's own crontab.
The usage is very simple, just add an extra delay time. After looking at the source code of huey, it is executed immediately by default. Of course, it still depends on whether all your threads are in a state of pending execution.
The code is as follows | |||||
>>> import datetime
>>> res |
代码如下 | |
# tasks.py from datetime import datetime from config import huey @huey.task(retries=3, retry_delay=10) def try_thrice(): print 'trying....%s' % datetime.now() raise Exception('nope') |
Here is another introduction to retry. Huey also has retry, which is a very practical thing. If you have seen my introduction to the celery retry mechanism in the above article, you should also understand what huey is about. Yes, he actually also made a decorator in front of the specific functions in tasks. There is a func try exception retry logic in the decorator. Everyone understands.
The code is as follows | |||||
# tasks.py from datetime import datetime
|
huey gives you a chance to regret it~ That is to say, after you complete deley's planned task, if you want to cancel it, you can just revoke it.
The code is as follows | |||||
# count some beans
res = count_beans(10000000)
|
The code is as follows | |
from config import huey from tasks import count_beans if __name__ == '__main__': beans = raw_input('How many beans? ') Count_beans(int(beans)) Print('Enqueued job to count %s beans' % beans) |
tasks.py
代码如下 | |||||||||||||
import random
from huey import crontab
def count_beans(num):
|
代码如下 | |
#!/bin/bash echo "HUEY CONSUMER" echo "-------------" echo "In another terminal, run 'python main.py'" echo "Stop the consumer using Ctrl+C" PYTHONPATH=.:$PYTHONPATH python ../../huey/bin/huey_consumer.py main.huey --threads=2 => |
代码如下 | |
[xiaorui@devops /tmp ]$ git clone https://github.com/coleifer/huey.git Cloning into 'huey'... remote: Counting objects: 1423, done. remote: Compressing objects: 100% (9/9), done. Receiving objects: 34% (497/1423), 388.00 KiB | 29.00 KiB/s KiB/s Receiving objects: 34% (498/1423), 628.00 KiB | 22.00 KiB/s remote: Total 1423 (delta 0), reused 0 (delta 0) Receiving objects: 100% (1423/1423), 2.24 MiB | 29.00 KiB/s, done. Resolving deltas: 100% (729/729), done. Checking connectivity... done. [xiaorui@devops /tmp ]$cd huey/examples/simple [xiaorui@devops simple (master)]$ ll total 40 -rw-r--r-- 1 xiaorui wheel 79B 9 8 08:49 README -rw-r--r-- 1 xiaorui wheel 0B 9 8 08:49 __init__.py -rw-r--r-- 1 xiaorui wheel 56B 9 8 08:49 config.py -rwxr-xr-x 1 xiaorui wheel 227B 9 8 08:49 cons.sh -rw-r--r-- 1 xiaorui wheel 205B 9 8 08:49 main.py -rw-r--r-- 1 xiaorui wheel 607B 9 8 08:49 tasks.py [xiaorui@devops simple (master)]$ |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
