使用python脚本安排sidekiq作业

问题描述 投票:0回答:2

我有一个带有rails APP的sidekiq。我需要能够从python脚本运行一个作业(因为我使用Luigi来运行一般的任务)。我正在寻找一个与Sidekiq API一起工作的python库,但到目前为止还没有运气。有关于此的任何想法或想法?

python api task sidekiq luigi
2个回答
2
投票

https://github.com/mperham/sidekiq/wiki/FAQ#how-do-i-push-a-job-to-sidekiq-without-ruby

这是最简单的Ruby,将其翻译为Python:

require 'securerandom'
require 'json'

redis = Redis.new(:url => 'redis://hostname:port/db')
msg = { "class" => 'MyWorker',
    "queue" => 'default',
    "args" => [1, 2, 3],
    'retry' => true,
    'jid' => SecureRandom.hex(12),
    'created_at' => Time.now.to_f,
    'enqueued_at' => Time.now.to_f }
redis.lpush("queue:default", JSON.dump(msg))

0
投票

这是Python的翻译:

import redis
import json

from random import choice
from datetime import datetime

sidekiq_queues = redis.Redis(host=host, port=port, db=db, password=password)

queue = 'my_queue'
job = 'MyJob'
arguments = 123456789

value = {
    "class": job,
    "queue": queue,
    "args": [arguments],
    "retry": 'true',
    "jid": ''.join([choice('qwertyuiopasdfghjklzxcvbnm1234567890') for i in range(24)]),
    "created_at": datetime.now().timestamp(),
    "enqueued_at": datetime.now().timestamp()}

sidekiq_queues.sadd(f"queues", queue)
sidekiq_queues.lpush(f"queue:{queue}" , json.dumps(value))
© www.soinside.com 2019 - 2024. All rights reserved.