debugging.rst 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 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 the worker encounters your breakpoint it will log the following
  31. information::
  32. [INFO/MainProcess] Received task:
  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. .. code-block:: bash
  41. $ telnet localhost 6900
  42. Connected to localhost.
  43. Escape character is '^]'.
  44. > /opt/devel/demoapp/tasks.py(128)add()
  45. -> return result
  46. (Pdb)
  47. Enter ``help`` to get a list of available commands,
  48. It may be a good idea to read the `Python Debugger Manual`_ if
  49. you have never used `pdb` before.
  50. To demonstrate, we will read the value of the ``result`` variable,
  51. change it and continue execution of the task::
  52. (Pdb) result
  53. 4
  54. (Pdb) result = 'hello from rdb'
  55. (Pdb) continue
  56. Connection closed by foreign host.
  57. The result of our vandalism can be seen in the worker logs::
  58. [2011-01-18 14:35:36,599: INFO/MainProcess] Task
  59. tasks.add[d7261c71-4962-47e5-b342-2448bedd20e8] succeeded
  60. in 61.481s: 'hello from rdb'
  61. .. _`Python Debugger Manual`: http://docs.python.org/library/pdb.html
  62. Tips
  63. ====
  64. .. _breakpoint_signal:
  65. Enabling the breakpoint signal
  66. ------------------------------
  67. If the environment variable :envvar:`CELERY_RDBSIG` is set, the worker
  68. will open up an rdb instance whenever the `SIGUSR2` signal is sent.
  69. This is the case for both main and worker processes.
  70. For example starting the worker with::
  71. CELERY_RDBSIG=1 celery worker -l info
  72. You can start an rdb session for any of the worker processes by executing::
  73. kill -USR2 <pid>