-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
42 lines (36 loc) · 1.01 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import rq
from rq import Worker, Connection, Queue
from rq.job import Job
from redis import Redis
from threading import Thread
from time import sleep
from task import run_task
def run():
task_id = push_task().id
sleep(5)
cancel_task(task_id)
def run_worker():
with Connection(Redis(
host='localhost',
port=6379
)) as connection:
worker = Worker("default", connection=connection)
worker.work()
def push_task():
with Connection(Redis(
host='localhost',
port=6379
)) as connection:
queue = Queue(connection=connection)
return queue.enqueue(f=run_task)
def cancel_task(job_id):
with Connection(Redis(
host='localhost',
port=6379
)) as connection:
job = Job.fetch(job_id)
print("Job status before cancel:", job.get_status())
rq.cancel_job(job_id=job_id, connection=connection)
print("Job status after cancel:", job.get_status())
if __name__ == "__main__":
run()