Debian Codegraph @

docs/workarounds.rst

raw
1 Known Issues and Workarounds
2 ----------------------------
3
4 Don't Call Twisted APIs from non-Twisted threads
5 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6
7 As is the case in any Twisted program, you should never call Twisted APIs (e.g. ``reactor.callLater``) from non-Twisted threads.
8 Only call Twisted APIs from functions decorated by ``@wait_for`` and friends.
9
10 Preventing deadlocks on shutdown
11 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12
13 To ensure a timely process exit, during reactor shutdown Crochet will try to
14 interrupt calls to ``EventualResult.wait()`` or functions decorated with
15 ``@wait_for`` with a ``crochet.ReactorStopped`` exception. This is still not a
16 complete solution, unfortunately. If you are shutting down a thread pool as
17 part of Twisted's reactor shutdown, this will wait until all threads are
18 done. If you're blocking indefinitely, this may rely on Crochet interrupting
19 those blocking calls... but Crochet's shutdown may be delayed until the thread
20 pool finishes shutting down, depending on the ordering of shutdown events.
21
22 The solution is to interrupt all blocking calls yourself. You can do this by
23 firing or canceling any ``Deferred`` instances you are waiting on as part of
24 your application shutdown, and do so before you stop any thread pools.
25
26 Reducing Twisted log messages
27 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
28
29 Twisted can be rather verbose with its log messages. If you wish to reduce the
30 message flow you can limit them to error messages only:
31
32 .. code-block:: python
33
34 import logging
35 logging.getLogger('twisted').setLevel(logging.ERROR)
36
37
38 Missing tracebacks
39 ^^^^^^^^^^^^^^^^^^
40
41 In order to prevent massive memory leaks, Twisted currently wipes out the traceback from exceptions it captures (see https://tm.tl/7873 for ideas on improving this).
42 This means that often exceptions re-raised by Crochet will be missing their tracebacks.
43 You can however get access to a string version of the traceback, suitable for logging, from ``EventualResult`` objects returned by ``@run_in_reactor``\-wrapped functions:
44
45 .. code-block:: python
46
47 from crochet import run_in_reactor, TimeoutError
48
49 @run_in_reactor
50 def download_page(url):
51 from twisted.web.client import getPage
52 return getPage(url)
53
54 result = download_page("https://github.com")
55 try:
56 page = result.wait(timeout=1000)
57 except TimeoutError:
58 # Handle timeout ...
59 except:
60 # Something else happened:
61 print(result.original_failure().getTraceback())
62
63 uWSGI, multiprocessing, Celery
64 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
65
66 uWSGI, the standard library ``multiprocessing.py`` library and Celery by default use ``fork()`` without ``exec()`` to create child processes on Unix systems.
67 This means they effectively clone a running parent Python process, preserving all existing imported modules.
68 This is a fundamentally broken thing to do, e.g. it breaks the standard library's ``logging`` package.
69 It also breaks Crochet.
70
71 You have two options for dealing with this problem.
72 The ideal solution is to avoid this "feature":
73
74 uWSGI
75 Use the ``--lazy-apps`` command-line option.
76
77 ``multiprocessing.py``
78 Use the ``spawn`` (or possibly ``forkserver``) start methods when using Python 3. See https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods for more details.
79
80 Alternatively, you can ensure you only start Crochet inside the child process:
81
82 uWSGI
83 Only run ``crochet.setup()`` inside the WSGI application function.
84
85 ``multiprocessing.py``
86 Only run ``crochet.setup()`` in the child process.
87
88 Celery
89 Only run ``crochet.setup()`` inside tasks.