Package: rearq
ReArqIntroductionReArq is a distributed task queue with asyncio and redis, which rewrite fromarqto make improvement and include web interface.You can tryDemo Onlinehere.FeaturesAsyncIO support, easy integration withFastAPI.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.ScreenshotsRequirementsRedis >= 5.0InstallUse MySQL backend:pipinstallrearq[mysql]Use PostgreSQL backend:pipinstallrearq[postgres]Quick StartTask Definition# main.pyfromrearqimportReArqrearq=ReArq(db_url='mysql://root:123456@127.0.0.1:3306/rearq')@rearq.on_shutdownasyncdefon_shutdown():# you can do some clean work here like close db and so on...print("shutdown")@rearq.on_startupasyncdefon_startup():# you should do some initialization work hereprint("startup")# you must init Tortoise ORM hereawaitTortoise.init(db_url=settings.DB_URL,modules={"rearq":["rearq.server.models"]},)@rearq.task(queue="q1")asyncdefadd(self,a,b):returna+b@rearq.task(cron="*/5 * * * * * *")# run task per 5 secondsasyncdeftimer(self):return"timer"Run rearq worker>rearqmain:rearqworker-qq1-qq2# consume tasks from q1 and q2 as the same time2021-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=6Run rearq timerIf you have timing task or delay task, you should run another command also:>rearqmain:rearqtimer2021-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=6Also, you can run timer with worker together byrearq main:rearq worker -t.Integration in FastAPIfromfastapiimportFastAPIapp=FastAPI()@app.on_event("startup")asyncdefstartup()->None:awaitTortoise.init(db_url=settings.DB_URL,modules={"rearq":["rearq.server.models"]},)@app.on_event("shutdown")asyncdefshutdown()->None:awaitrearq.close()# then run task in view@app.get("/test")asyncdeftest():job=awaitadd.delay(args=(1,2))# orjob=awaitadd.delay(kwargs={"a":1,"b":2})# orjob=awaitadd.delay(1,2)# orjob=awaitadd.delay(a=1,b=2)result=awaitjob.result(timeout=5)# wait result for 5 secondsprint(result.result)returnresultStart web interface>rearqmain:rearqserver
Usage:rearqserver[OPTIONS]Startrestapiserver.

Options:--hostTEXTListenhost.[default:0.0.0.0]-p,--portINTEGERListenport.[default:8000]-h,--helpShowthismessageandexit..After server run, you can visithttps://127.0.0.1:8000/docsto see all apis
andhttps://127.0.0.1:8000to see web interface.Other options will pass intouvicorndirectly, such as--root-pathetc.rearqmain:rearqserver--host0.0.0.0--root-path/rearqMount as FastAPI sub appYou can also mount rearq server as FastAPI sub app.fromfastapiimportFastAPIfromexamples.tasksimportrearqfromrearq.server.appimportappasrearq_appapp=FastAPI()app.mount("/rearq",rearq_app)rearq_app.set_rearq(rearq)Start worker inside appYou can also start worker inside your app.@app.on_event("startup")asyncdefstartup():awaitrearq.init()awaitrearq_app.start_worker(with_timer=True,block=False)ThanksToarq, Fast job queuing and RPC in python with asyncio and redis.LicenseThis project is licensed under theApache-2.0License.