简体   繁体   English

计划任务执行中的芹菜错误

[英]Celery error in scheduled task execution

I have an instance of Celery running with Redis server in an archlinux machine. 我在Archlinux机器上有一个运行Redis服务器的Celery实例。 In this instance I have defined some tasks to be executed every X minutes. 在这种情况下,我定义了一些每X分钟执行一次的任务。 The schedule is working correctly (if I check the logs, the tasks are being called), but there is a task outputting an error. 日程安排正常(如果我检查日志,则正在调用任务),但是有一个任务输出错误。 The task is to run another python script. 任务是运行另一个python脚本。 If I run this script manually, it works perfectly . 如果我手动运行此脚本,则可以正常运行 But when celery try to execute it, I receive this error: 但是当芹菜尝试执行它时,我收到此错误:

WARNING/ForkPoolWorker-93] Exception in thread Thread-6:
Traceback (most recent call last):
  File "/usr/lib64/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/usr/lib64/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
TypeError: 'int' object is not callable

The tasks.py is: task.py是:

@periodic_task(
    run_every=(crontab(minute='*/15')),
    name="sm37_auto",
    ignore_result=True
)
def sm37_auto():
    comando_sm37 = os.system("python /home/user/sm37.py")
    background_thread=threading.Thread(target=comando_sm37)
    background_thread.start()
    return 'ejecuta SM37 ok'

Could somebody help me? 有人可以帮我吗?

I suppose, you are talking about /home/user/sm37.py which works perfectly. 我想,您在谈论的是/home/user/sm37.py ,它可以完美运行。 The script task.py will execute fine as well because it has nothing to execute: syntactically the function is correct. 脚本task.py也可以很好地执行,因为它没有什么要执行的:语法上该函数是正确的。

The problem is that when you start a thread, you need to pass something executable as target and you pass a return value instead. 问题在于,启动线程时,您需要传递一些可执行文件作为target而传递一个返回值。 Consider making a change: 考虑进行更改:

def sm37_thread():
    comando_sm37 = os.system("python /home/user/sm37.py")

def sm37_auto():
    background_thread=threading.Thread(target=sm37_thread)
    background_thread.start()
    return 'ejecuta SM37 ok'

Here: 这里:

comando_sm37 = os.system("python /home/user/sm37.py")

this executes the /home/user/sm37.py scripts and returns the process exit status, which is an integer error code (where 0 means 'no error' and everything else is an error). 这将执行/home/user/sm37.py脚本并返回进程退出状态,该状态为整数错误代码(其中0表示“无错误”,其他所有内容均为错误)。

Then you have: 然后您有:

background_thread=threading.Thread(
    target=comando_sm37
)

where you try to create a thread (why you want to do so in this context is way beyond my imagination but that's another topic), passing an integer as the target callable. 在其中尝试创建线程的地方(为什么要在这种情况下这样做超出了我的想象,但这是另一个主题),并传递了一个整数作为可调用目标。

To make a long story short, this code makes no sense at all. 长话短说,这段代码毫无意义。

Using os.system to execute a Python script is already quite questionable (python code can import other python code, you don't need os.system for this) but well, you might have reasons to do so (legacy code, whatever...). 使用os.system执行Python脚本已经很成问题(python代码可以导入其他python代码,您不需要os.system ),但是,您可能有这样做的理由(旧版代码,无论如何。) )。 But starting a thread from a celery task, really ??? 但是从芹菜任务开始线程,真的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM