如何调试在 Eclipse 中本地运行的 Celery/Django 任务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12698212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 19:07:46  来源:igfitidea点击:

How to debug Celery/Django tasks running locally in Eclipse

pythoneclipsecelery

提问by spoonboy

I need to debug Celery task from the Eclipse debugger. I'm using Eclipse, PyDev and Django.

我需要从 Eclipse 调试器调试 Celery 任务。我正在使用 Eclipse、PyDev 和 Django。

First, I open my project in Eclipse and put a breakpoint at the beginning of the task function.

首先,我在 Eclipse 中打开我的项目并在任务函数的开头放置一个断点。

Then, I'm starting the Celery workers from Eclipse by Right Clicking on manage.py from the PyDev Package Explorer and choosing "Debug As->Python Run" and specifying "celeryd -l info" as the argument. This starts MainThread, Mediator and three more threads visible from the Eclipse debugger.

然后,我通过在 PyDev 包资源管理器中右键单击 manage.py 并选择“Debug As->Python Run”并指定“celeryd -l info”作为参数,从 Eclipse 启动 Celery 工作器。这将启动 MainThread、Mediator 和另外三个可从 Eclipse 调试器中看到的线程。

After that I return back to the PyDev view and start the main application by Right Click on the project and choosing Run As/PyDev:Django

之后我返回 PyDev 视图并通过右键单击项目并选择 Run As/PyDev:Django 来启动主应用程序

My issues is that once the task is submitted by the mytask.delay() it doesn't stop on the breakpoint. I put some traces withing the tasks code so I can see that it was executed in one of the worker threads.

我的问题是,一旦任务由 mytask.delay() 提交,它就不会在断点处停止。我在任务代码中添加了一些痕迹,以便我可以看到它是在其中一个工作线程中执行的。

So, how to make the Eclipse debugger to stop on the breakpoint placed withing the task when it executed in the Celery workers thread?

那么,当它在 Celery 工作线程中执行时,如何让 Eclipse 调试器在任务放置的断点处停止?

回答by Tommaso Barbugli

You should consider the option to run the celery task in the same thread as the main process (normally it runs on a separate process), this will make the debug much easier.

您应该考虑在与主进程相同的线程中运行 celery 任务的选项(通常它在单独的进程上运行),这将使调试更容易。

You can tell celery to run the task in sync by adding this setting to your settings.py module:

您可以通过将此设置添加到 settings.py 模块来告诉 celery 同步运行任务:

CELERY_TASK_ALWAYS_EAGER  = True
# use this if you are on older versions of celery
# CELERY_ALWAYS_EAGER = True 

Note: this is only meant to be in use for debugging or development stages!

注意:这仅用于调试或开发阶段!

回答by seddonym

You can do it using Celery's rdb:

您可以使用 Celery 的rdb来做到这一点:

from celery.contrib import rdb
rdb.set_trace()

Then, in a different terminal type telnet localhost 6900, and you will get the debug prompt.

然后,在不同的终端类型中telnet localhost 6900,您将获得调试提示。

回答by sherbang

CELERYD_POOLdefaults to celery.concurrency.prefork:TaskPoolwhich will spawn separate processes for each worker and PyDev can't see inside them. If you change it to one of the threaded options then you can use the debugger.

CELERYD_POOL默认为celery.concurrency.prefork:TaskPool哪个,将为每个工作人员生成单独的进程,而 PyDev 无法看到其中的内容。如果将其更改为线程选项之一,则可以使用调试器。

For example, for Celery 3.1 you can use this setting:

例如,对于 Celery 3.1,您可以使用此设置:

CELERYD_POOL = 'celery.concurrency.threads:TaskPool'

Note that this requires the threadpoolmodule to be installed.

请注意,这需要安装线程池模块。

Also make sure to have CELERY_ALWAYS_EAGER = False, otherwise changing the pool class makes no sense.

还要确保有CELERY_ALWAYS_EAGER = False,否则更改池类是没有意义的。

回答by eugene

I create a management command to test task.. find it easier than running it from shell..

我创建了一个管理命令来测试任务..发现它比从 shell 运行它更容易..

回答by Fabio Zadrozny

If it runs only on a different thread, it should work on the latest PyDev versions (I think there was an issue before where a spawned thread would not be debugged, but this was fixed).

如果它只在不同的线程上运行,它应该可以在最新的 PyDev 版本上运行(我认为之前存在一个问题,即无法调试生成的线程,但已修复)。

Now, if it's launching on a different process, you need to use the remote debugger (even if it's on the same machine). See: http://pydev.org/manual_adv_remote_debugger.html

现在,如果它在不同的进程上启动,您需要使用远程调试器(即使它在同一台机器上)。请参阅:http: //pydev.org/manual_adv_remote_debugger.html