私は常にcronを実行するためにセロリの定期タスクを使用していますが、今回はセロリのcrontabが自分のcronジョブを実行できません。私はだけが火曜日に20:50から21:10まで20秒ごとに実行されるクロムが必要です。これを達成できないようですPythonは20秒ごとにcronジョブを実行します
-1
A
答えて
0
あなたのpythonスクリプトをシステムのcrontabに追加することも、あなた自身でコード化することもできます。火曜日の20時50分までの時間を計算し、それまでスリープしてからジョブを実行し、残りの秒間スリープし、21時10分が再び火曜日20時50分までの時間を計算する新しいスレッドを開始します。
これは、コマンドtime.time()
とtime.sleep()
を使用して簡単に実行できます。
0
誰でもセロリのcrontabを数秒で実行できるようにしたい場合は、以下は、crontabを拡張した例です。
from celery.schedules import crontab, cronfield
from celery.utils.timeutils import ffwd
CRON_REPR = '''\
<crontab: {0._orig_second} {0._orig_minute} {0._orig_hour} {0._orig_day_of_week} \
{0._orig_day_of_month} {0._orig_month_of_year} (s/m/h/d/dM/MY)>\
'''
class ExtendedCrontab(crontab):
'''
custom crontab to support 'seconds'
'''
def __init__(self, second=0, minute='*', hour='*', day_of_week='*',
day_of_month='*', month_of_year='*', nowfun=None, app=None):
super().__init__(second, minute, hour, day_of_week, day_of_month,
month_of_year, nowfun, app)
self._orig_second = cronfield(second)
self.second = self._expand_cronspec(second, 60)
def _delta_to_next(self, last_run_at, next_hour, next_minute, next_second):
'''
Takes a datetime of last run, next minute and hour, and
returns a relativedelta for the next scheduled day and time.
Only called when day_of_month and/or month_of_year cronspec
is specified to further limit scheduled task execution.
'''
_ffwd = super()._delta_to_next(last_run_at, next_hour, next_minute)
_ffwd.second = next_second
return _ffwd
def __repr__(self):
return CRON_REPR.format(self)
def __reduce__(self):
return (self.__class__, (self._orig_second,
self._orig_minute,
self._orig_hour,
self._orig_day_of_week,
self._orig_day_of_month,
self._orig_month_of_year), None)
def remaining_delta(self, last_run_at, tz=None, ffwd=ffwd):
tz = tz or self.tz
last_run_at = self.maybe_make_aware(last_run_at)
now = self.maybe_make_aware(self.now())
dow_num = last_run_at.isoweekday() % 7 # Sunday is day 0, not day 7
execute_this_date = (last_run_at.month in self.month_of_year and
last_run_at.day in self.day_of_month and
dow_num in self.day_of_week)
execute_this_hour = (execute_this_date and
last_run_at.day == now.day and
last_run_at.month == now.month and
last_run_at.year == now.year and
last_run_at.hour in self.hour and
last_run_at.minute < max(self.minute))
execute_this_minute = (last_run_at.minute in self.minute and
last_run_at.second < max(self.second))
if execute_this_minute:
next_second = min(second for second in self.second
if second > last_run_at.second)
delta = ffwd(second=next_second, microsecond=0)
else:
if execute_this_hour:
next_minute = min(minute for minute in self.minute
if minute > last_run_at.minute)
next_second = min(self.second)
delta = ffwd(minute=next_minute, second=next_second, microsecond=0)
else:
next_minute = min(self.minute)
next_second = min(self.second)
execute_today = (execute_this_date and
last_run_at.hour < max(self.hour))
if execute_today:
next_hour = min(hour for hour in self.hour
if hour > last_run_at.hour)
delta = ffwd(hour=next_hour, minute=next_minute,
second=next_second, microsecond=0)
else:
next_hour = min(self.hour)
all_dom_moy = (self._orig_day_of_month == '*' and
self._orig_month_of_year == '*')
if all_dom_moy:
next_day = min([day for day in self.day_of_week
if day > dow_num] or self.day_of_week)
add_week = next_day == dow_num
delta = ffwd(weeks=add_week and 1 or 0,
weekday=(next_day - 1) % 7,
hour=next_hour,
minute=next_minute,
second=next_second,
microsecond=0)
else:
delta = self._delta_to_next(last_run_at,
next_hour, next_minute,
next_second)
return self.to_local(last_run_at), delta, self.to_local(now)
def __eq__(self, other):
if isinstance(other, crontab):
return (other.month_of_year == self.month_of_year and
other.day_of_month == self.day_of_month and
other.day_of_week == self.day_of_week and
other.hour == self.hour and
other.minute == self.minute and
other.second == self.second)
return NotImplemented
ExtendedCrontab(second=second,
minute=minute,
hour=hour,
day_of_week=day_of_week,
day_of_month=day_of_month,
month_of_year=month_of_year)
関連する問題
- 1. Cron:1秒ごとにcronを実行していますか?
- 2. cronジョブを 'x'秒ごとに実行することができます
- 3. cronは、私はcronジョブを実行するごとに15分
- 4. cronジョブは、私は5分ごとに実行するcronジョブを持って
- 5. y秒ごとにx秒間pythonサブプロセスを実行します
- 6. cronジョブなしで1分ごとにphpスクリプトを実行
- 7. 40秒ごとにジョブを起動するQuartz Cronトリガー
- 8. インタラクティブなPythonスクリプトをcronジョブとして実行するには?
- 9. node-cronジョブを3時間ごとに実行する
- 10. ジョブごとに異なる時刻にcronジョブを実行する
- 11. ユーザーごとにDevise Cronジョブ
- 12. 6時間ごとにlinuxでcronジョブを実行
- 13. cron macでphpスクリプトを実行する - 10秒ごと
- 14. php cronジョブ10分ごと
- 15. Cronジョブを実行する
- 16. 他のcronジョブが終了したらcronジョブを実行するには?
- 17. スクリプトを20分ごとに実行
- 18. PythonスクリプトとCronジョブ
- 19. symfonyでcronジョブを実行
- 20. 5分ごとにcronジョブを作成しますか?
- 21. Cronコマンドは60秒ごとにphpスクリプトを実行してからすべてをオーバーロードします
- 22. Cronジョブ:最後の20行を維持する
- 23. mySQLクエリをcronジョブとして実行しますか?
- 24. AndroidはX秒ごとにタスクを実行します
- 25. 起動エージェントは10秒ごとにスクリプトを実行します
- 26. 関数は20分ごとに実行されます
- 27. cronを20分ごとにではなく、時間
- 28. rsyncでcronジョブを実行すると、ジョブはスタックされますか?
- 29. 1ミリ秒ごとにコードを実行
- 30. 15秒ごとにAPIリクエストを実行