• Stars
    star
    147
  • Rank 250,349 (Top 5 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created over 4 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A distributed task queue built with asyncio and redis, with built-in web interface

ReArq

image image image image

Introduction

ReArq is a distributed task queue with asyncio and redis, which rewrite from arq to make improvement and include web interface.

You can try Demo Online here.

Features

  • AsyncIO support, easy integration with FastAPI.
  • Delay task, cron task and async task support.
  • Full-featured build-in web interface.
  • Built-in distributed task lock to make same task only run one at the same time.
  • Other powerful features to be discovered.

Screenshots

dashboard worker task job result

Requirements

  • Redis >= 5.0

Install

Use MySQL backend:

pip install rearq[mysql]

Use PostgreSQL backend:

pip install rearq[postgres]

Quick Start

Task Definition

# main.py
from rearq import ReArq

rearq = ReArq(db_url='mysql://root:[email protected]:3306/rearq')


@rearq.on_shutdown
async def on_shutdown():
    # you can do some clean work here like close db and so on...
    print("shutdown")


@rearq.on_startup
async def on_startup():
    # you should do some initialization work here
    print("startup")
    # you must init Tortoise ORM here
    await Tortoise.init(
        db_url=settings.DB_URL,
        modules={"rearq": ["rearq.server.models"]},
    )


@rearq.task(queue="q1")
async def add(self, a, b):
    return a + b


@rearq.task(cron="*/5 * * * * * *")  # run task per 5 seconds
async def timer(self):
    return "timer"

Run rearq worker

> rearq main:rearq worker -q q1 -q q2 # consume tasks from q1 and q2 as the same time
2021-03-29 09:54:50.464 | INFO     | rearq.worker:_main:95 - Start worker success with queue: rearq:queue:default
2021-03-29 09:54:50.465 | INFO     | rearq.worker:_main:96 - Registered tasks: add, sleep, timer_add
2021-03-29 09:54:50.465 | INFO     | rearq.worker:log_redis_info:86 - redis_version=6.2.1 mem_usage=1.43M clients_connected=5 db_keys=6

Run rearq timer

If you have timing task or delay task, you should run another command also:

> rearq main:rearq timer
2021-03-29 09:54:43.878 | INFO     | rearq.worker:_main:275 - Start timer success
2021-03-29 09:54:43.887 | INFO     | rearq.worker:_main:277 - Registered timer tasks: timer_add
2021-03-29 09:54:43.894 | INFO     | rearq.worker:log_redis_info:86 - redis_version=6.2.1 mem_usage=1.25M clients_connected=2 db_keys=6

Also, you can run timer with worker together by rearq main:rearq worker -t.

Integration in FastAPI

from fastapi import FastAPI

app = FastAPI()


@app.on_event("startup")
async def startup() -> None:
    await Tortoise.init(
        db_url=settings.DB_URL,
        modules={"rearq": ["rearq.server.models"]},
    )


@app.on_event("shutdown")
async def shutdown() -> None:
    await rearq.close()


# then run task in view
@app.get("/test")
async def test():
    job = await add.delay(args=(1, 2))
    # or
    job = await add.delay(kwargs={"a": 1, "b": 2})
    # or
    job = await add.delay(1, 2)
    # or
    job = await add.delay(a=1, b=2)
    result = await job.result(timeout=5)  # wait result for 5 seconds
    print(result.result)
    return result

Start web interface

> rearq main:rearq server
Usage: rearq server [OPTIONS]

  Start rest api server.

Options:
  --host TEXT         Listen host.  [default: 0.0.0.0]
  -p, --port INTEGER  Listen port.  [default: 8000]
  -h, --help          Show this message and exit..

After server run, you can visit https://127.0.0.1:8000/docs to see all apis and https://127.0.0.1:8000 to see web interface.

Other options will pass into uvicorn directly, such as --root-path etc.

rearq main:rearq server --host 0.0.0.0 --root-path /rearq

Mount as FastAPI sub app

You can also mount rearq server as FastAPI sub app.

from fastapi import FastAPI

from examples.tasks import rearq
from rearq.server.app import app as rearq_app

app = FastAPI()

app.mount("/rearq", rearq_app)
rearq_app.set_rearq(rearq)

Start worker inside app

You can also start worker inside your app.

@app.on_event("startup")
async def startup():
    await rearq.init()
    await rearq_app.start_worker(with_timer=True, block=False)

ThanksTo

  • arq, Fast job queuing and RPC in python with asyncio and redis.

License

This project is licensed under the Apache-2.0 License.

More Repositories

1

fastapi-cache

fastapi-cache is a tool to cache fastapi response and function result, with backends support redis and memcached.
Python
1,264
star
2

fastapi-limiter

A request rate limiter for fastapi
Python
452
star
3

synch

Sync data from the other DB to ClickHouse(cluster)
Python
345
star
4

asyncmy

A fast asyncio MySQL/MariaDB driver with replication protocol support
Python
234
star
5

meilisync

Realtime sync data from MySQL/PostgreSQL/MongoDB to Meilisearch
Python
230
star
6

asynch

An asyncio ClickHouse Python Driver with native (TCP) interface support.
Python
169
star
7

swagin

Swagger + Gin = SwaGin, a web framework based on Gin and Swagger
Go
68
star
8

trader

A framework that automated cryptocurrency exchange with strategy
Go
62
star
9

databack

Backup your data from MySQL/PostgreSQL/SSH etc. to any other storages
Python
60
star
10

longurl

A self-hosted short url service
Go
51
star
11

meilisync-admin

A web admin dashboard for meilisync
Python
31
star
12

fibers

Fiber + Swagger = Fibers, a web framework dedicated to providing a FastAPI-like development experience
Go
26
star
13

alarmer

A tool focus on error reporting for your application, like sentry but lightweight
Python
19
star
14

fastapi-rest

Fast restful API based on FastAPI and TortoiseORM
Python
10
star
15

gema-web

Convert from json/xml/yaml to Pydantic/Go/Rust etc.
TypeScript
9
star
16

awesome-web

Search awesome projects
TypeScript
8
star
17

chcli

A Terminal Client for ClickHouse with AutoCompletion and Syntax Highlighting.
Python
6
star
18

fettler

Auto refresh cache of redis with MySQL binlog
Python
4
star
19

telsearch-web

Frontend for telsearch
TypeScript
3
star
20

mccabe

Calculate the cyclomatic complexity of the source code
Python
3
star
21

s3web

Serve static files from any S3 compatible object storage endpoints
Go
2
star
22

ClashForiOS

Swift
2
star
23

xiaoai

小爱音箱非官方SDK
Python
2
star
24

awesome

Search for awesome github project
Go
2
star
25

longurl-web

This is frontend for https://github.com/long2ice/longurl
TypeScript
2
star
26

ergo

Python
2
star
27

gema

Convert from json/xml/yaml to Pydantic/Go/Rust etc.
Python
2
star
28

nvim

My personal neovim config
Lua
2
star
29

sponsor

Sponsor page of long2ice
HTML
2
star
30

youtube-dl-api-server

api server for youtube-dl
Python
2
star
31

talkit

A self hosted comment system
1
star
32

long2ice

My Personal README.
1
star
33

vpsmon

Python
1
star
34

homepage

My homepage
TypeScript
1
star
35

kanp

See video by download
Python
1
star
36

devme

Python
1
star
37

creatable

A tool to create table from file/database to another database
Python
1
star
38

devme-web

TypeScript
1
star
39

hugo-theme-pure

Forked from https://github.com/xiaoheiAh/hugo-theme-pure and make improvements
CSS
1
star
40

fastapi-monitor

Python
1
star
41

meilisync-web

Frontend of meilisearch-admin
Vue
1
star