debugging.rst 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 import task
  13. from celery.contrib import rdb
  14. @task()
  15. def add(x, y):
  16. result = x + y
  17. rdb.set_trace() # <- set break-point
  18. return result
  19. :func:`~celery.contrib.rdb.set_trace` sets a break-point 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 the worker encounters your break-point it will log the following
  31. information:
  32. .. code-block:: text
  33. [INFO/MainProcess] Received task:
  34. tasks.add[d7261c71-4962-47e5-b342-2448bedd20e8]
  35. [WARNING/PoolWorker-1] Remote Debugger:6900:
  36. Please telnet 127.0.0.1 6900. Type `exit` in session to continue.
  37. [2011-01-18 14:25:44,119: WARNING/PoolWorker-1] Remote Debugger:6900:
  38. Waiting for client...
  39. If you telnet the port specified you will be presented
  40. with a `pdb` shell:
  41. .. code-block:: console
  42. $ telnet localhost 6900
  43. Connected to localhost.
  44. Escape character is '^]'.
  45. > /opt/devel/demoapp/tasks.py(128)add()
  46. -> return result
  47. (Pdb)
  48. Enter ``help`` to get a list of available commands,
  49. It may be a good idea to read the `Python Debugger Manual`_ if
  50. you have never used `pdb` before.
  51. To demonstrate, we will read the value of the ``result`` variable,
  52. change it and continue execution of the task:
  53. .. code-block:: text
  54. (Pdb) result
  55. 4
  56. (Pdb) result = 'hello from rdb'
  57. (Pdb) continue
  58. Connection closed by foreign host.
  59. The result of our vandalism can be seen in the worker logs:
  60. .. code-block:: text
  61. [2011-01-18 14:35:36,599: INFO/MainProcess] Task
  62. tasks.add[d7261c71-4962-47e5-b342-2448bedd20e8] succeeded
  63. in 61.481s: 'hello from rdb'
  64. .. _`Python Debugger Manual`: http://docs.python.org/library/pdb.html
  65. Tips
  66. ====
  67. .. _breakpoint_signal:
  68. Enabling the break-point signal
  69. -------------------------------
  70. If the environment variable :envvar:`CELERY_RDBSIG` is set, the worker
  71. will open up an rdb instance whenever the `SIGUSR2` signal is sent.
  72. This is the case for both main and worker processes.
  73. For example starting the worker with:
  74. .. code-block:: console
  75. $ CELERY_RDBSIG=1 celery worker -l info
  76. You can start an rdb session for any of the worker processes by executing:
  77. .. code-block:: console
  78. $ kill -USR2 <pid>