debugging.rst 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. .. _tut-remote_debug:
  2. ======================================
  3. Debugging Tasks Remotely (using pdb)
  4. ======================================
  5. :mod:`celery.contrib.rdb` is an extended version of :mod:`pdb` that
  6. enables remote debugging of processes that does not have terminal
  7. access.
  8. Example usage:
  9. .. code-block:: python
  10. from celery.contrib import rdb
  11. from celery.task import task
  12. @task
  13. def add(x, y):
  14. result = x + y
  15. rdb.set_trace() # <- set breakpoint
  16. return result
  17. :func:`~celery.contrib.rdb.set_trace` sets a breakpoint at the current
  18. location and creates a socket you can telnet into to remotely debug
  19. your task.
  20. The debugger may be started by multiple processes at the same time,
  21. so rather than using a fixed port the debugger will search for an
  22. available port, starting from the base port (6900 by default).
  23. The base port can be changed using the environment variable
  24. :envvar:`CELERY_RDB_PORT`.
  25. By default the debugger will only be available from the local host,
  26. to enable access from the outside you have to set the environment
  27. variable :envvar:`CELERY_RDB_HOST`.
  28. When `celeryd` encounters your breakpoint it will log the following
  29. information::
  30. [INFO/MainProcess] Got task from broker:
  31. tasks.add[d7261c71-4962-47e5-b342-2448bedd20e8]
  32. [WARNING/PoolWorker-1] Remote Debugger:6900:
  33. Please telnet 127.0.0.1 6900. Type `exit` in session to continue.
  34. [2011-01-18 14:25:44,119: WARNING/PoolWorker-1] Remote Debugger:6900:
  35. Waiting for client...
  36. If you telnet the port specified you will be presented
  37. with a `pdb` shell::
  38. $ telnet localhost 6900
  39. Connected to localhost.
  40. Escape character is '^]'.
  41. > /opt/devel/demoapp/tasks.py(128)add()
  42. -> return result
  43. (Pdb)
  44. Enter ``help`` to get a list of available commands,
  45. It may be a good idea to read the `Python Debugger Manual`_ if
  46. you have never used `pdb` before.
  47. To demonstrate, we will read the value of the ``result`` variable,
  48. change it and continue execution of the task::
  49. (Pdb) result
  50. 4
  51. (Pdb) result = "hello from rdb"
  52. (Pdb) continue
  53. Connection closed by foreign host.
  54. The result of our vandalism can be seen in the worker logs::
  55. [2011-01-18 14:35:36,599: INFO/MainProcess] Task
  56. tasks.add[d7261c71-4962-47e5-b342-2448bedd20e8] succeeded
  57. in 61.481s: 'hello from rdb'
  58. .. _`Python Debugger Manual`: http://docs.python.org/library/pdb.html