debugging.rst 3.1 KB

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