最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】celery调用task的apply_async传递参数出错:TypeError takes 1 positional argument but 40 were given

celery crifan 6259浏览 0评论

折腾:

【已解决】Mac本地用Celery实现延时执行任务

期间,用代码:

<code># celery_task.py
from celery import Celery
import os

# app = Celery('tasks', broker='redis://localhost//')
app = Celery('tasks', broker='redis://localhost')

@app.task()
def add(x, y):
   return x + y

@app.task()
def deleteTmpAudioFile(filename):
    # audioTmpFolder = app.config["AUDIO_TEMP_FOLDER"]
    print("deleteTmpAudioFile: filename=%s", filename)
    audioTmpFolder = "tmp/audio"
    print("audioTmpFolder=%s" % audioTmpFolder)
    curFolderAbsPath = os.getcwd() #'/Users/crifan/dev/dev_root/company/naturling/projects/robotDemo/server'
    print("curFolderAbsPath=%s" % curFolderAbsPath)
    audioTmpFolderFullPath = os.path.join(curFolderAbsPath, audioTmpFolder)
    print("audioTmpFolderFullPath=%s" % audioTmpFolderFullPath)
    tempAudioFullname = os.path.join(audioTmpFolderFullPath, filename)
    #'/Users/crifan/dev/dev_root/company/naturling/projects/robotDemo/server/tmp/audio/2aba73d1-f8d0-4302-9dd3-d1dbfad44458.mp3'
    if os.path.isfile(tempAudioFullname):
        os.remove(tempAudioFullname)
        print("Ok to delete file %s" % tempAudioFullname)
    else:
        print("No need to remove for not exist file %s" % tempAudioFullname)
</code>

然后参考:

异步任务神器 Celery | FunHacks

celery.app.task — Celery 4.1.0 documentation

去调用,结果出错

<code>➜  server python
Python 3.6.4 (default, Mar 22 2018, 13:54:22)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import celery_task
&gt;&gt;&gt; from celery_task import deleteTmpAudioFile
&gt;&gt;&gt; deleteTmpAudioFile.apply_async(args=("98fc7c46-7aa0-4dd7-aa9d-89fdf516abd6.mp3"), countdown=10)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/Users/crifan/.local/share/virtualenvs/server-9an_1rEM/lib/python3.6/site-packages/celery/app/task.py", line 518, in apply_async
    check_arguments(*(args or ()), **(kwargs or {}))
TypeError: deleteTmpAudioFile() takes 1 positional argument but 40 were given
</code>

换成:

<code>&gt;&gt;&gt; deleteTmpAudioFile.apply_async(args="98fc7c46-7aa0-4dd7-aa9d-89fdf516abd6.mp3", countdown=10)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/Users/crifan/.local/share/virtualenvs/server-9an_1rEM/lib/python3.6/site-packages/celery/app/task.py", line 518, in apply_async
    check_arguments(*(args or ()), **(kwargs or {}))
TypeError: deleteTmpAudioFile() takes 1 positional argument but 40 were given
</code>

继续:

<code>&gt;&gt;&gt; deleteTmpAudioFile.apply_async(args={"filename":"98fc7c46-7aa0-4dd7-aa9d-89fdf516abd6.mp3"}, countdown=10)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/Users/crifan/.local/share/virtualenvs/server-9an_1rEM/lib/python3.6/site-packages/celery/app/task.py", line 536, in apply_async
    **options
  File "/Users/crifan/.local/share/virtualenvs/server-9an_1rEM/lib/python3.6/site-packages/celery/app/base.py", line 729, in send_task
    root_id, parent_id, shadow, chain,
  File "/Users/crifan/.local/share/virtualenvs/server-9an_1rEM/lib/python3.6/site-packages/celery/app/amqp.py", line 316, in as_task_v2
    raise TypeError('task args must be a list or tuple')
TypeError: task args must be a list or tuple
</code>

* args (Tuple) – The positional arguments to pass on to the task.

* kwargs (Dict) – The keyword arguments to pass on to the task.

celery apply_async TypeError  takes 1 positional argument but 40 were given

celery apply_async TypeError  takes 1 positional argument

python – TypeError: method() takes 1 positional argument but 2 were given – Stack Overflow

celery apply_async 参数

#sora#celery笔记——call the task – Hochikong的Blog

<code>&gt;&gt;&gt; deleteTmpAudioFile.apply_async(args="98fc7c46-7aa0-4dd7-aa9d-89fdf516abd6.mp3", countdown=10)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/Users/crifan/.local/share/virtualenvs/server-9an_1rEM/lib/python3.6/site-packages/celery/app/task.py", line 518, in apply_async
    check_arguments(*(args or ()), **(kwargs or {}))
TypeError: deleteTmpAudioFile() takes 1 positional argument but 40 were given
</code>
<code>&gt;&gt;&gt; deleteTmpAudioFile.apply_async(args=(98fc7c46-7aa0-4dd7-aa9d-89fdf516abd6.mp3), countdown=10)
  File "&lt;stdin&gt;", line 1
    deleteTmpAudioFile.apply_async(args=(98fc7c46-7aa0-4dd7-aa9d-89fdf516abd6.mp3), countdown=10)
                                                ^
SyntaxError: invalid syntax
</code>
<code>&gt;&gt;&gt; deleteTmpAudioFile.apply_async(("98fc7c46-7aa0-4dd7-aa9d-89fdf516abd6.mp3"), countdown=10)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/Users/crifan/.local/share/virtualenvs/server-9an_1rEM/lib/python3.6/site-packages/celery/app/task.py", line 518, in apply_async
    check_arguments(*(args or ()), **(kwargs or {}))
TypeError: deleteTmpAudioFile() takes 1 positional argument but 40 were given
</code>
<code>&gt;&gt;&gt; deleteTmpAudioFile.apply_async("98fc7c46-7aa0-4dd7-aa9d-89fdf516abd6.mp3", countdown=10)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/Users/crifan/.local/share/virtualenvs/server-9an_1rEM/lib/python3.6/site-packages/celery/app/task.py", line 518, in apply_async
    check_arguments(*(args or ()), **(kwargs or {}))
TypeError: deleteTmpAudioFile() takes 1 positional argument but 40 were given
</code>
<code>&gt;&gt;&gt; deleteTmpAudioFile.apply_async("a")
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/Users/crifan/.local/share/virtualenvs/server-9an_1rEM/lib/python3.6/site-packages/celery/app/task.py", line 536, in apply_async
    **options
  File "/Users/crifan/.local/share/virtualenvs/server-9an_1rEM/lib/python3.6/site-packages/celery/app/base.py", line 729, in send_task
    root_id, parent_id, shadow, chain,
  File "/Users/crifan/.local/share/virtualenvs/server-9an_1rEM/lib/python3.6/site-packages/celery/app/amqp.py", line 316, in as_task_v2
    raise TypeError('task args must be a list or tuple')
TypeError: task args must be a list or tuple
</code>

始终不行。

celery apply_async 用法

<code>&gt;&gt;&gt; deleteTmpAudioFile.apply_async(("a"))
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/Users/crifan/.local/share/virtualenvs/server-9an_1rEM/lib/python3.6/site-packages/celery/app/task.py", line 536, in apply_async
    **options
  File "/Users/crifan/.local/share/virtualenvs/server-9an_1rEM/lib/python3.6/site-packages/celery/app/base.py", line 729, in send_task
    root_id, parent_id, shadow, chain,
  File "/Users/crifan/.local/share/virtualenvs/server-9an_1rEM/lib/python3.6/site-packages/celery/app/amqp.py", line 316, in as_task_v2
    raise TypeError('task args must be a list or tuple')
TypeError: task args must be a list or tuple
</code>

最后改为list终于可以了:

<code>&gt;&gt;&gt; deleteTmpAudioFile.apply_async(["98fc7c46-7aa0-4dd7-aa9d-89fdf516abd6.mp3"], countdown=10)
&lt;AsyncResult: 53608319-b87f-4f06-8483-e8f7d1c99632&gt;
</code>

【总结】

最后的最后,终于搞清楚了:

<code>&gt;&gt;&gt; deleteTmpAudioFile.apply_async(("98fc7c46-7aa0-4dd7-aa9d-89fdf516abd6.mp3",), countdown=10)
&lt;AsyncResult: bd89951c-2078-49af-b8ea-8c9d8d5b121a&gt;
</code>

也是可以的。

而对于之前的提示:

TypeError: task args must be a list or tuple

指的是:apply_async的参数,第一个是args,必须是:tuple或list

所以传递参数,list写成:

<code>["98fc7c46-7aa0-4dd7-aa9d-89fdf516abd6.mp3"]
</code>

是可以的。

而此处,之所以:

<code>("98fc7c46-7aa0-4dd7-aa9d-89fdf516abd6.mp3")
</code>

不行,而:

<code>("98fc7c46-7aa0-4dd7-aa9d-89fdf516abd6.mp3",)
</code>

却是可以的。

去测试了下,才明白:

<code>&gt;&gt;&gt; type(("abc"))
&lt;class 'str'&gt;
&gt;&gt;&gt; type(("abc",))
&lt;class 'tuple'&gt;
</code>

很明显,python中对于:

<code>("abc")
</code>

会被识别为字符串

<code>("abc", )
</code>

才会被识别为tuple。

所以,此处写成:

list的:

<code>deleteTmpAudioFile.apply_async(["98fc7c46-7aa0-4dd7-aa9d-89fdf516abd6.mp3"], countdown=10)
</code>

tuple的:

<code>deleteTmpAudioFile.apply_async(("98fc7c46-7aa0-4dd7-aa9d-89fdf516abd6.mp3",), countdown=10)
</code>

才可以。

转载请注明:在路上 » 【已解决】celery调用task的apply_async传递参数出错:TypeError takes 1 positional argument but 40 were given

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.194 seconds, using 22.19MB memory