debugging.rst 2.8 KB

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