~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-3.5.1-docs-html/library/asyncio-eventloop.html

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
 
 
4
 
 
5
 
<html xmlns="http://www.w3.org/1999/xhtml">
6
 
  <head>
7
 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
8
 
    
9
 
    <title>18.5.1. Base Event Loop &mdash; Python 3.5.1 documentation</title>
10
 
    
11
 
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
12
 
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
13
 
    
14
 
    <script type="text/javascript">
15
 
      var DOCUMENTATION_OPTIONS = {
16
 
        URL_ROOT:    '../',
17
 
        VERSION:     '3.5.1',
18
 
        COLLAPSE_INDEX: false,
19
 
        FILE_SUFFIX: '.html',
20
 
        HAS_SOURCE:  true
21
 
      };
22
 
    </script>
23
 
    <script type="text/javascript" src="../_static/jquery.js"></script>
24
 
    <script type="text/javascript" src="../_static/underscore.js"></script>
25
 
    <script type="text/javascript" src="../_static/doctools.js"></script>
26
 
    <script type="text/javascript" src="../_static/sidebar.js"></script>
27
 
    <link rel="search" type="application/opensearchdescription+xml"
28
 
          title="Search within Python 3.5.1 documentation"
29
 
          href="../_static/opensearch.xml"/>
30
 
    <link rel="author" title="About these documents" href="../about.html" />
31
 
    <link rel="copyright" title="Copyright" href="../copyright.html" />
32
 
    <link rel="top" title="Python 3.5.1 documentation" href="../contents.html" />
33
 
    <link rel="up" title="18.5. asyncio – Asynchronous I/O, event loop, coroutines and tasks" href="asyncio.html" />
34
 
    <link rel="next" title="18.5.2. Event loops" href="asyncio-eventloops.html" />
35
 
    <link rel="prev" title="18.5. asyncio – Asynchronous I/O, event loop, coroutines and tasks" href="asyncio.html" />
36
 
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
 
    <script type="text/javascript" src="../_static/copybutton.js"></script>
38
 
    <script type="text/javascript" src="../_static/version_switch.js"></script>
39
 
    
40
 
 
41
 
 
42
 
  </head>
43
 
  <body role="document">  
44
 
    <div class="related" role="navigation" aria-label="related navigation">
45
 
      <h3>Navigation</h3>
46
 
      <ul>
47
 
        <li class="right" style="margin-right: 10px">
48
 
          <a href="../genindex.html" title="General Index"
49
 
             accesskey="I">index</a></li>
50
 
        <li class="right" >
51
 
          <a href="../py-modindex.html" title="Python Module Index"
52
 
             >modules</a> |</li>
53
 
        <li class="right" >
54
 
          <a href="asyncio-eventloops.html" title="18.5.2. Event loops"
55
 
             accesskey="N">next</a> |</li>
56
 
        <li class="right" >
57
 
          <a href="asyncio.html" title="18.5. asyncio – Asynchronous I/O, event loop, coroutines and tasks"
58
 
             accesskey="P">previous</a> |</li>
59
 
        <li><img src="../_static/py.png" alt=""
60
 
                 style="vertical-align: middle; margin-top: -1px"/></li>
61
 
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
62
 
        <li>
63
 
          <span class="version_switcher_placeholder">3.5.1</span>
64
 
          <a href="../index.html">Documentation </a> &raquo;
65
 
        </li>
66
 
 
67
 
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
68
 
          <li class="nav-item nav-item-2"><a href="ipc.html" >18. Interprocess Communication and Networking</a> &raquo;</li>
69
 
          <li class="nav-item nav-item-3"><a href="asyncio.html" accesskey="U">18.5. <code class="docutils literal"><span class="pre">asyncio</span></code> &#8211; Asynchronous I/O, event loop, coroutines and tasks</a> &raquo;</li> 
70
 
      </ul>
71
 
    </div>    
72
 
 
73
 
    <div class="document">
74
 
      <div class="documentwrapper">
75
 
        <div class="bodywrapper">
76
 
          <div class="body" role="main">
77
 
            
78
 
  <div class="section" id="base-event-loop">
79
 
<span id="asyncio-event-loop"></span><h1>18.5.1. Base Event Loop<a class="headerlink" href="#base-event-loop" title="Permalink to this headline">¶</a></h1>
80
 
<p>The event loop is the central execution device provided by <a class="reference internal" href="asyncio.html#module-asyncio" title="asyncio: Asynchronous I/O, event loop, coroutines and tasks."><code class="xref py py-mod docutils literal"><span class="pre">asyncio</span></code></a>.
81
 
It provides multiple facilities, including:</p>
82
 
<ul class="simple">
83
 
<li>Registering, executing and cancelling delayed calls (timeouts).</li>
84
 
<li>Creating client and server <a class="reference internal" href="asyncio-protocol.html#asyncio-transport"><span>transports</span></a> for various
85
 
kinds of communication.</li>
86
 
<li>Launching subprocesses and the associated <a class="reference internal" href="asyncio-protocol.html#asyncio-transport"><span>transports</span></a> for communication with an external program.</li>
87
 
<li>Delegating costly function calls to a pool of threads.</li>
88
 
</ul>
89
 
<dl class="class">
90
 
<dt id="asyncio.BaseEventLoop">
91
 
<em class="property">class </em><code class="descclassname">asyncio.</code><code class="descname">BaseEventLoop</code><a class="headerlink" href="#asyncio.BaseEventLoop" title="Permalink to this definition">¶</a></dt>
92
 
<dd><p>Base class of event loops.</p>
93
 
<p>This class is <a class="reference internal" href="asyncio-dev.html#asyncio-multithreading"><span>not thread safe</span></a>.</p>
94
 
</dd></dl>
95
 
 
96
 
<div class="section" id="run-an-event-loop">
97
 
<h2>18.5.1.1. Run an event loop<a class="headerlink" href="#run-an-event-loop" title="Permalink to this headline">¶</a></h2>
98
 
<dl class="method">
99
 
<dt id="asyncio.BaseEventLoop.run_forever">
100
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">run_forever</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.run_forever" title="Permalink to this definition">¶</a></dt>
101
 
<dd><p>Run until <a class="reference internal" href="#asyncio.BaseEventLoop.stop" title="asyncio.BaseEventLoop.stop"><code class="xref py py-meth docutils literal"><span class="pre">stop()</span></code></a> is called.  If <a class="reference internal" href="#asyncio.BaseEventLoop.stop" title="asyncio.BaseEventLoop.stop"><code class="xref py py-meth docutils literal"><span class="pre">stop()</span></code></a> is called before
102
 
<a class="reference internal" href="#asyncio.BaseEventLoop.run_forever" title="asyncio.BaseEventLoop.run_forever"><code class="xref py py-meth docutils literal"><span class="pre">run_forever()</span></code></a> is called, this polls the I/O selector once
103
 
with a timeout of zero, runs all callbacks scheduled in response to
104
 
I/O events (and those that were already scheduled), and then exits.
105
 
If <a class="reference internal" href="#asyncio.BaseEventLoop.stop" title="asyncio.BaseEventLoop.stop"><code class="xref py py-meth docutils literal"><span class="pre">stop()</span></code></a> is called while <a class="reference internal" href="#asyncio.BaseEventLoop.run_forever" title="asyncio.BaseEventLoop.run_forever"><code class="xref py py-meth docutils literal"><span class="pre">run_forever()</span></code></a> is running,
106
 
this will run the current batch of callbacks and then exit.  Note
107
 
that callbacks scheduled by callbacks will not run in that case;
108
 
they will run the next time <a class="reference internal" href="#asyncio.BaseEventLoop.run_forever" title="asyncio.BaseEventLoop.run_forever"><code class="xref py py-meth docutils literal"><span class="pre">run_forever()</span></code></a> is called.</p>
109
 
<div class="versionchanged">
110
 
<p><span class="versionmodified">Changed in version 3.5.1.</span></p>
111
 
</div>
112
 
</dd></dl>
113
 
 
114
 
<dl class="method">
115
 
<dt id="asyncio.BaseEventLoop.run_until_complete">
116
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">run_until_complete</code><span class="sig-paren">(</span><em>future</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.run_until_complete" title="Permalink to this definition">¶</a></dt>
117
 
<dd><p>Run until the <a class="reference internal" href="asyncio-task.html#asyncio.Future" title="asyncio.Future"><code class="xref py py-class docutils literal"><span class="pre">Future</span></code></a> is done.</p>
118
 
<p>If the argument is a <a class="reference internal" href="asyncio-task.html#coroutine"><span>coroutine object</span></a>, it is wrapped by
119
 
<a class="reference internal" href="asyncio-task.html#asyncio.ensure_future" title="asyncio.ensure_future"><code class="xref py py-func docutils literal"><span class="pre">ensure_future()</span></code></a>.</p>
120
 
<p>Return the Future&#8217;s result, or raise its exception.</p>
121
 
</dd></dl>
122
 
 
123
 
<dl class="method">
124
 
<dt id="asyncio.BaseEventLoop.is_running">
125
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">is_running</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.is_running" title="Permalink to this definition">¶</a></dt>
126
 
<dd><p>Returns running status of event loop.</p>
127
 
</dd></dl>
128
 
 
129
 
<dl class="method">
130
 
<dt id="asyncio.BaseEventLoop.stop">
131
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">stop</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.stop" title="Permalink to this definition">¶</a></dt>
132
 
<dd><p>Stop running the event loop.</p>
133
 
<p>This causes <a class="reference internal" href="#asyncio.BaseEventLoop.run_forever" title="asyncio.BaseEventLoop.run_forever"><code class="xref py py-meth docutils literal"><span class="pre">run_forever()</span></code></a> to exit at the next suitable
134
 
opportunity (see there for more details).</p>
135
 
<div class="versionchanged">
136
 
<p><span class="versionmodified">Changed in version 3.5.1.</span></p>
137
 
</div>
138
 
</dd></dl>
139
 
 
140
 
<dl class="method">
141
 
<dt id="asyncio.BaseEventLoop.is_closed">
142
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">is_closed</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.is_closed" title="Permalink to this definition">¶</a></dt>
143
 
<dd><p>Returns <code class="docutils literal"><span class="pre">True</span></code> if the event loop was closed.</p>
144
 
<div class="versionadded">
145
 
<p><span class="versionmodified">New in version 3.4.2.</span></p>
146
 
</div>
147
 
</dd></dl>
148
 
 
149
 
<dl class="method">
150
 
<dt id="asyncio.BaseEventLoop.close">
151
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.close" title="Permalink to this definition">¶</a></dt>
152
 
<dd><p>Close the event loop. The loop must not be running.  Pending
153
 
callbacks will be lost.</p>
154
 
<p>This clears the queues and shuts down the executor, but does not wait for
155
 
the executor to finish.</p>
156
 
<p>This is idempotent and irreversible. No other methods should be called after
157
 
this one.</p>
158
 
</dd></dl>
159
 
 
160
 
</div>
161
 
<div class="section" id="calls">
162
 
<span id="asyncio-pass-keywords"></span><h2>18.5.1.2. Calls<a class="headerlink" href="#calls" title="Permalink to this headline">¶</a></h2>
163
 
<p>Most <a class="reference internal" href="asyncio.html#module-asyncio" title="asyncio: Asynchronous I/O, event loop, coroutines and tasks."><code class="xref py py-mod docutils literal"><span class="pre">asyncio</span></code></a> functions don&#8217;t accept keywords. If you want to pass
164
 
keywords to your callback, use <a class="reference internal" href="functools.html#functools.partial" title="functools.partial"><code class="xref py py-func docutils literal"><span class="pre">functools.partial()</span></code></a>. For example,
165
 
<code class="docutils literal"><span class="pre">loop.call_soon(functools.partial(print,</span> <span class="pre">&quot;Hello&quot;,</span> <span class="pre">flush=True))</span></code> will call
166
 
<code class="docutils literal"><span class="pre">print(&quot;Hello&quot;,</span> <span class="pre">flush=True)</span></code>.</p>
167
 
<div class="admonition note">
168
 
<p class="first admonition-title">Note</p>
169
 
<p class="last"><a class="reference internal" href="functools.html#functools.partial" title="functools.partial"><code class="xref py py-func docutils literal"><span class="pre">functools.partial()</span></code></a> is better than <code class="docutils literal"><span class="pre">lambda</span></code> functions, because
170
 
<a class="reference internal" href="asyncio.html#module-asyncio" title="asyncio: Asynchronous I/O, event loop, coroutines and tasks."><code class="xref py py-mod docutils literal"><span class="pre">asyncio</span></code></a> can inspect <a class="reference internal" href="functools.html#functools.partial" title="functools.partial"><code class="xref py py-func docutils literal"><span class="pre">functools.partial()</span></code></a> object to display
171
 
parameters in debug mode, whereas <code class="docutils literal"><span class="pre">lambda</span></code> functions have a poor
172
 
representation.</p>
173
 
</div>
174
 
<dl class="method">
175
 
<dt id="asyncio.BaseEventLoop.call_soon">
176
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">call_soon</code><span class="sig-paren">(</span><em>callback</em>, <em>*args</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.call_soon" title="Permalink to this definition">¶</a></dt>
177
 
<dd><p>Arrange for a callback to be called as soon as possible.  The callback is
178
 
called after <a class="reference internal" href="#asyncio.BaseEventLoop.call_soon" title="asyncio.BaseEventLoop.call_soon"><code class="xref py py-meth docutils literal"><span class="pre">call_soon()</span></code></a> returns, when control returns to the event
179
 
loop.</p>
180
 
<p>This operates as a FIFO queue, callbacks are called in the order in
181
 
which they are registered.  Each callback will be called exactly once.</p>
182
 
<p>Any positional arguments after the callback will be passed to the
183
 
callback when it is called.</p>
184
 
<p>An instance of <a class="reference internal" href="#asyncio.Handle" title="asyncio.Handle"><code class="xref py py-class docutils literal"><span class="pre">asyncio.Handle</span></code></a> is returned, which can be
185
 
used to cancel the callback.</p>
186
 
<p><a class="reference internal" href="#asyncio-pass-keywords"><span>Use functools.partial to pass keywords to the callback</span></a>.</p>
187
 
</dd></dl>
188
 
 
189
 
<dl class="method">
190
 
<dt id="asyncio.BaseEventLoop.call_soon_threadsafe">
191
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">call_soon_threadsafe</code><span class="sig-paren">(</span><em>callback</em>, <em>*args</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.call_soon_threadsafe" title="Permalink to this definition">¶</a></dt>
192
 
<dd><p>Like <a class="reference internal" href="#asyncio.BaseEventLoop.call_soon" title="asyncio.BaseEventLoop.call_soon"><code class="xref py py-meth docutils literal"><span class="pre">call_soon()</span></code></a>, but thread safe.</p>
193
 
<p>See the <a class="reference internal" href="asyncio-dev.html#asyncio-multithreading"><span>concurrency and multithreading</span></a>
194
 
section of the documentation.</p>
195
 
</dd></dl>
196
 
 
197
 
</div>
198
 
<div class="section" id="delayed-calls">
199
 
<span id="asyncio-delayed-calls"></span><h2>18.5.1.3. Delayed calls<a class="headerlink" href="#delayed-calls" title="Permalink to this headline">¶</a></h2>
200
 
<p>The event loop has its own internal clock for computing timeouts.
201
 
Which clock is used depends on the (platform-specific) event loop
202
 
implementation; ideally it is a monotonic clock.  This will generally be
203
 
a different clock than <a class="reference internal" href="time.html#time.time" title="time.time"><code class="xref py py-func docutils literal"><span class="pre">time.time()</span></code></a>.</p>
204
 
<div class="admonition note">
205
 
<p class="first admonition-title">Note</p>
206
 
<p class="last">Timeouts (relative <em>delay</em> or absolute <em>when</em>) should not exceed one day.</p>
207
 
</div>
208
 
<dl class="method">
209
 
<dt id="asyncio.BaseEventLoop.call_later">
210
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">call_later</code><span class="sig-paren">(</span><em>delay</em>, <em>callback</em>, <em>*args</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.call_later" title="Permalink to this definition">¶</a></dt>
211
 
<dd><p>Arrange for the <em>callback</em> to be called after the given <em>delay</em>
212
 
seconds (either an int or float).</p>
213
 
<p>An instance of <a class="reference internal" href="#asyncio.Handle" title="asyncio.Handle"><code class="xref py py-class docutils literal"><span class="pre">asyncio.Handle</span></code></a> is returned, which can be
214
 
used to cancel the callback.</p>
215
 
<p><em>callback</em> will be called exactly once per call to <a class="reference internal" href="#asyncio.BaseEventLoop.call_later" title="asyncio.BaseEventLoop.call_later"><code class="xref py py-meth docutils literal"><span class="pre">call_later()</span></code></a>.
216
 
If two callbacks are scheduled for exactly the same time, it is
217
 
undefined which will be called first.</p>
218
 
<p>The optional positional <em>args</em> will be passed to the callback when it
219
 
is called. If you want the callback to be called with some named
220
 
arguments, use a closure or <a class="reference internal" href="functools.html#functools.partial" title="functools.partial"><code class="xref py py-func docutils literal"><span class="pre">functools.partial()</span></code></a>.</p>
221
 
<p><a class="reference internal" href="#asyncio-pass-keywords"><span>Use functools.partial to pass keywords to the callback</span></a>.</p>
222
 
</dd></dl>
223
 
 
224
 
<dl class="method">
225
 
<dt id="asyncio.BaseEventLoop.call_at">
226
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">call_at</code><span class="sig-paren">(</span><em>when</em>, <em>callback</em>, <em>*args</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.call_at" title="Permalink to this definition">¶</a></dt>
227
 
<dd><p>Arrange for the <em>callback</em> to be called at the given absolute timestamp
228
 
<em>when</em> (an int or float), using the same time reference as
229
 
<a class="reference internal" href="#asyncio.BaseEventLoop.time" title="asyncio.BaseEventLoop.time"><code class="xref py py-meth docutils literal"><span class="pre">BaseEventLoop.time()</span></code></a>.</p>
230
 
<p>This method&#8217;s behavior is the same as <a class="reference internal" href="#asyncio.BaseEventLoop.call_later" title="asyncio.BaseEventLoop.call_later"><code class="xref py py-meth docutils literal"><span class="pre">call_later()</span></code></a>.</p>
231
 
<p>An instance of <a class="reference internal" href="#asyncio.Handle" title="asyncio.Handle"><code class="xref py py-class docutils literal"><span class="pre">asyncio.Handle</span></code></a> is returned, which can be
232
 
used to cancel the callback.</p>
233
 
<p><a class="reference internal" href="#asyncio-pass-keywords"><span>Use functools.partial to pass keywords to the callback</span></a>.</p>
234
 
</dd></dl>
235
 
 
236
 
<dl class="method">
237
 
<dt id="asyncio.BaseEventLoop.time">
238
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">time</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.time" title="Permalink to this definition">¶</a></dt>
239
 
<dd><p>Return the current time, as a <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal"><span class="pre">float</span></code></a> value, according to the
240
 
event loop&#8217;s internal clock.</p>
241
 
</dd></dl>
242
 
 
243
 
<div class="admonition seealso">
244
 
<p class="first admonition-title">See also</p>
245
 
<p class="last">The <a class="reference internal" href="asyncio-task.html#asyncio.sleep" title="asyncio.sleep"><code class="xref py py-func docutils literal"><span class="pre">asyncio.sleep()</span></code></a> function.</p>
246
 
</div>
247
 
</div>
248
 
<div class="section" id="tasks">
249
 
<h2>18.5.1.4. Tasks<a class="headerlink" href="#tasks" title="Permalink to this headline">¶</a></h2>
250
 
<dl class="method">
251
 
<dt id="asyncio.BaseEventLoop.create_task">
252
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">create_task</code><span class="sig-paren">(</span><em>coro</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.create_task" title="Permalink to this definition">¶</a></dt>
253
 
<dd><p>Schedule the execution of a <a class="reference internal" href="asyncio-task.html#coroutine"><span>coroutine object</span></a>: wrap it in
254
 
a future. Return a <a class="reference internal" href="asyncio-task.html#asyncio.Task" title="asyncio.Task"><code class="xref py py-class docutils literal"><span class="pre">Task</span></code></a> object.</p>
255
 
<p>Third-party event loops can use their own subclass of <a class="reference internal" href="asyncio-task.html#asyncio.Task" title="asyncio.Task"><code class="xref py py-class docutils literal"><span class="pre">Task</span></code></a> for
256
 
interoperability. In this case, the result type is a subclass of
257
 
<a class="reference internal" href="asyncio-task.html#asyncio.Task" title="asyncio.Task"><code class="xref py py-class docutils literal"><span class="pre">Task</span></code></a>.</p>
258
 
<p>This method was added in Python 3.4.2. Use the <a class="reference internal" href="asyncio-task.html#asyncio.async" title="asyncio.async"><code class="xref py py-func docutils literal"><span class="pre">async()</span></code></a> function to
259
 
support also older Python versions.</p>
260
 
<div class="versionadded">
261
 
<p><span class="versionmodified">New in version 3.4.2.</span></p>
262
 
</div>
263
 
</dd></dl>
264
 
 
265
 
<dl class="method">
266
 
<dt id="asyncio.BaseEventLoop.set_task_factory">
267
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">set_task_factory</code><span class="sig-paren">(</span><em>factory</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.set_task_factory" title="Permalink to this definition">¶</a></dt>
268
 
<dd><p>Set a task factory that will be used by
269
 
<a class="reference internal" href="#asyncio.BaseEventLoop.create_task" title="asyncio.BaseEventLoop.create_task"><code class="xref py py-meth docutils literal"><span class="pre">BaseEventLoop.create_task()</span></code></a>.</p>
270
 
<p>If <em>factory</em> is <code class="docutils literal"><span class="pre">None</span></code> the default task factory will be set.</p>
271
 
<p>If <em>factory</em> is a <em>callable</em>, it should have a signature matching
272
 
<code class="docutils literal"><span class="pre">(loop,</span> <span class="pre">coro)</span></code>, where <em>loop</em> will be a reference to the active
273
 
event loop, <em>coro</em> will be a coroutine object.  The callable
274
 
must return an <a class="reference internal" href="asyncio-task.html#asyncio.Future" title="asyncio.Future"><code class="xref py py-class docutils literal"><span class="pre">asyncio.Future</span></code></a> compatible object.</p>
275
 
<div class="versionadded">
276
 
<p><span class="versionmodified">New in version 3.4.4.</span></p>
277
 
</div>
278
 
</dd></dl>
279
 
 
280
 
<dl class="method">
281
 
<dt id="asyncio.BaseEventLoop.get_task_factory">
282
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">get_task_factory</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.get_task_factory" title="Permalink to this definition">¶</a></dt>
283
 
<dd><p>Return a task factory, or <code class="docutils literal"><span class="pre">None</span></code> if the default one is in use.</p>
284
 
<div class="versionadded">
285
 
<p><span class="versionmodified">New in version 3.4.4.</span></p>
286
 
</div>
287
 
</dd></dl>
288
 
 
289
 
</div>
290
 
<div class="section" id="creating-connections">
291
 
<h2>18.5.1.5. Creating connections<a class="headerlink" href="#creating-connections" title="Permalink to this headline">¶</a></h2>
292
 
<dl class="method">
293
 
<dt id="asyncio.BaseEventLoop.create_connection">
294
 
<em class="property">coroutine </em><code class="descclassname">BaseEventLoop.</code><code class="descname">create_connection</code><span class="sig-paren">(</span><em>protocol_factory</em>, <em>host=None</em>, <em>port=None</em>, <em>*</em>, <em>ssl=None</em>, <em>family=0</em>, <em>proto=0</em>, <em>flags=0</em>, <em>sock=None</em>, <em>local_addr=None</em>, <em>server_hostname=None</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.create_connection" title="Permalink to this definition">¶</a></dt>
295
 
<dd><p>Create a streaming transport connection to a given Internet <em>host</em> and
296
 
<em>port</em>: socket family <a class="reference internal" href="socket.html#socket.AF_INET" title="socket.AF_INET"><code class="xref py py-data docutils literal"><span class="pre">AF_INET</span></code></a> or
297
 
<a class="reference internal" href="socket.html#socket.AF_INET6" title="socket.AF_INET6"><code class="xref py py-data docutils literal"><span class="pre">AF_INET6</span></code></a> depending on <em>host</em> (or <em>family</em> if specified),
298
 
socket type <a class="reference internal" href="socket.html#socket.SOCK_STREAM" title="socket.SOCK_STREAM"><code class="xref py py-data docutils literal"><span class="pre">SOCK_STREAM</span></code></a>.  <em>protocol_factory</em> must be a
299
 
callable returning a <a class="reference internal" href="asyncio-protocol.html#asyncio-protocol"><span>protocol</span></a> instance.</p>
300
 
<p>This method is a <a class="reference internal" href="asyncio-task.html#coroutine"><span>coroutine</span></a> which will try to
301
 
establish the connection in the background.  When successful, the
302
 
coroutine returns a <code class="docutils literal"><span class="pre">(transport,</span> <span class="pre">protocol)</span></code> pair.</p>
303
 
<p>The chronological synopsis of the underlying operation is as follows:</p>
304
 
<ol class="arabic simple">
305
 
<li>The connection is established, and a <a class="reference internal" href="asyncio-protocol.html#asyncio-transport"><span>transport</span></a>
306
 
is created to represent it.</li>
307
 
<li><em>protocol_factory</em> is called without arguments and must return a
308
 
<a class="reference internal" href="asyncio-protocol.html#asyncio-protocol"><span>protocol</span></a> instance.</li>
309
 
<li>The protocol instance is tied to the transport, and its
310
 
<code class="xref py py-meth docutils literal"><span class="pre">connection_made()</span></code> method is called.</li>
311
 
<li>The coroutine returns successfully with the <code class="docutils literal"><span class="pre">(transport,</span> <span class="pre">protocol)</span></code>
312
 
pair.</li>
313
 
</ol>
314
 
<p>The created transport is an implementation-dependent bidirectional stream.</p>
315
 
<div class="admonition note">
316
 
<p class="first admonition-title">Note</p>
317
 
<p class="last"><em>protocol_factory</em> can be any kind of callable, not necessarily
318
 
a class.  For example, if you want to use a pre-created
319
 
protocol instance, you can pass <code class="docutils literal"><span class="pre">lambda:</span> <span class="pre">my_protocol</span></code>.</p>
320
 
</div>
321
 
<p>Options that change how the connection is created:</p>
322
 
<ul>
323
 
<li><p class="first"><em>ssl</em>: if given and not false, a SSL/TLS transport is created
324
 
(by default a plain TCP transport is created).  If <em>ssl</em> is
325
 
a <a class="reference internal" href="ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal"><span class="pre">ssl.SSLContext</span></code></a> object, this context is used to create
326
 
the transport; if <em>ssl</em> is <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a>, a context with some
327
 
unspecified default settings is used.</p>
328
 
<div class="admonition seealso">
329
 
<p class="first admonition-title">See also</p>
330
 
<p class="last"><a class="reference internal" href="ssl.html#ssl-security"><span>SSL/TLS security considerations</span></a></p>
331
 
</div>
332
 
</li>
333
 
<li><p class="first"><em>server_hostname</em>, is only for use together with <em>ssl</em>,
334
 
and sets or overrides the hostname that the target server&#8217;s certificate
335
 
will be matched against.  By default the value of the <em>host</em> argument
336
 
is used.  If <em>host</em> is empty, there is no default and you must pass a
337
 
value for <em>server_hostname</em>.  If <em>server_hostname</em> is an empty
338
 
string, hostname matching is disabled (which is a serious security
339
 
risk, allowing for man-in-the-middle-attacks).</p>
340
 
</li>
341
 
<li><p class="first"><em>family</em>, <em>proto</em>, <em>flags</em> are the optional address family, protocol
342
 
and flags to be passed through to getaddrinfo() for <em>host</em> resolution.
343
 
If given, these should all be integers from the corresponding
344
 
<a class="reference internal" href="socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal"><span class="pre">socket</span></code></a> module constants.</p>
345
 
</li>
346
 
<li><p class="first"><em>sock</em>, if given, should be an existing, already connected
347
 
<a class="reference internal" href="socket.html#socket.socket" title="socket.socket"><code class="xref py py-class docutils literal"><span class="pre">socket.socket</span></code></a> object to be used by the transport.
348
 
If <em>sock</em> is given, none of <em>host</em>, <em>port</em>, <em>family</em>, <em>proto</em>, <em>flags</em>
349
 
and <em>local_addr</em> should be specified.</p>
350
 
</li>
351
 
<li><p class="first"><em>local_addr</em>, if given, is a <code class="docutils literal"><span class="pre">(local_host,</span> <span class="pre">local_port)</span></code> tuple used
352
 
to bind the socket to locally.  The <em>local_host</em> and <em>local_port</em>
353
 
are looked up using getaddrinfo(), similarly to <em>host</em> and <em>port</em>.</p>
354
 
</li>
355
 
</ul>
356
 
<div class="versionchanged">
357
 
<p><span class="versionmodified">Changed in version 3.5: </span>On Windows with <a class="reference internal" href="asyncio-eventloops.html#asyncio.ProactorEventLoop" title="asyncio.ProactorEventLoop"><code class="xref py py-class docutils literal"><span class="pre">ProactorEventLoop</span></code></a>, SSL/TLS is now supported.</p>
358
 
</div>
359
 
<div class="admonition seealso">
360
 
<p class="first admonition-title">See also</p>
361
 
<p class="last">The <a class="reference internal" href="asyncio-stream.html#asyncio.open_connection" title="asyncio.open_connection"><code class="xref py py-func docutils literal"><span class="pre">open_connection()</span></code></a> function can be used to get a pair of
362
 
(<a class="reference internal" href="asyncio-stream.html#asyncio.StreamReader" title="asyncio.StreamReader"><code class="xref py py-class docutils literal"><span class="pre">StreamReader</span></code></a>, <a class="reference internal" href="asyncio-stream.html#asyncio.StreamWriter" title="asyncio.StreamWriter"><code class="xref py py-class docutils literal"><span class="pre">StreamWriter</span></code></a>) instead of a protocol.</p>
363
 
</div>
364
 
</dd></dl>
365
 
 
366
 
<dl class="method">
367
 
<dt id="asyncio.BaseEventLoop.create_datagram_endpoint">
368
 
<em class="property">coroutine </em><code class="descclassname">BaseEventLoop.</code><code class="descname">create_datagram_endpoint</code><span class="sig-paren">(</span><em>protocol_factory</em>, <em>local_addr=None</em>, <em>remote_addr=None</em>, <em>*</em>, <em>family=0</em>, <em>proto=0</em>, <em>flags=0</em>, <em>reuse_address=None</em>, <em>reuse_port=None</em>, <em>allow_broadcast=None</em>, <em>sock=None</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.create_datagram_endpoint" title="Permalink to this definition">¶</a></dt>
369
 
<dd><p>Create datagram connection: socket family <a class="reference internal" href="socket.html#socket.AF_INET" title="socket.AF_INET"><code class="xref py py-data docutils literal"><span class="pre">AF_INET</span></code></a> or
370
 
<a class="reference internal" href="socket.html#socket.AF_INET6" title="socket.AF_INET6"><code class="xref py py-data docutils literal"><span class="pre">AF_INET6</span></code></a> depending on <em>host</em> (or <em>family</em> if specified),
371
 
socket type <a class="reference internal" href="socket.html#socket.SOCK_DGRAM" title="socket.SOCK_DGRAM"><code class="xref py py-data docutils literal"><span class="pre">SOCK_DGRAM</span></code></a>. <em>protocol_factory</em> must be a
372
 
callable returning a <a class="reference internal" href="asyncio-protocol.html#asyncio-protocol"><span>protocol</span></a> instance.</p>
373
 
<p>This method is a <a class="reference internal" href="asyncio-task.html#coroutine"><span>coroutine</span></a> which will try to
374
 
establish the connection in the background.  When successful, the
375
 
coroutine returns a <code class="docutils literal"><span class="pre">(transport,</span> <span class="pre">protocol)</span></code> pair.</p>
376
 
<p>Options changing how the connection is created:</p>
377
 
<ul class="simple">
378
 
<li><em>local_addr</em>, if given, is a <code class="docutils literal"><span class="pre">(local_host,</span> <span class="pre">local_port)</span></code> tuple used
379
 
to bind the socket to locally.  The <em>local_host</em> and <em>local_port</em>
380
 
are looked up using <a class="reference internal" href="#asyncio.BaseEventLoop.getaddrinfo" title="asyncio.BaseEventLoop.getaddrinfo"><code class="xref py py-meth docutils literal"><span class="pre">getaddrinfo()</span></code></a>.</li>
381
 
<li><em>remote_addr</em>, if given, is a <code class="docutils literal"><span class="pre">(remote_host,</span> <span class="pre">remote_port)</span></code> tuple used
382
 
to connect the socket to a remote address.  The <em>remote_host</em> and
383
 
<em>remote_port</em> are looked up using <a class="reference internal" href="#asyncio.BaseEventLoop.getaddrinfo" title="asyncio.BaseEventLoop.getaddrinfo"><code class="xref py py-meth docutils literal"><span class="pre">getaddrinfo()</span></code></a>.</li>
384
 
<li><em>family</em>, <em>proto</em>, <em>flags</em> are the optional address family, protocol
385
 
and flags to be passed through to <a class="reference internal" href="#asyncio.BaseEventLoop.getaddrinfo" title="asyncio.BaseEventLoop.getaddrinfo"><code class="xref py py-meth docutils literal"><span class="pre">getaddrinfo()</span></code></a> for <em>host</em>
386
 
resolution. If given, these should all be integers from the
387
 
corresponding <a class="reference internal" href="socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal"><span class="pre">socket</span></code></a> module constants.</li>
388
 
<li><em>reuse_address</em> tells the kernel to reuse a local socket in
389
 
TIME_WAIT state, without waiting for its natural timeout to
390
 
expire. If not specified will automatically be set to True on
391
 
UNIX.</li>
392
 
<li><em>reuse_port</em> tells the kernel to allow this endpoint to be bound to the
393
 
same port as other existing endpoints are bound to, so long as they all
394
 
set this flag when being created. This option is not supported on Windows
395
 
and some UNIX&#8217;s. If the <code class="xref py py-data docutils literal"><span class="pre">SO_REUSEPORT</span></code> constant is not
396
 
defined then this capability is unsupported.</li>
397
 
<li><em>allow_broadcast</em> tells the kernel to allow this endpoint to send
398
 
messages to the broadcast address.</li>
399
 
<li><em>sock</em> can optionally be specified in order to use a preexisting,
400
 
already connected, <a class="reference internal" href="socket.html#socket.socket" title="socket.socket"><code class="xref py py-class docutils literal"><span class="pre">socket.socket</span></code></a> object to be used by the
401
 
transport. If specified, <em>local_addr</em> and <em>remote_addr</em> should be omitted
402
 
(must be <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal"><span class="pre">None</span></code></a>).</li>
403
 
</ul>
404
 
<p>On Windows with <a class="reference internal" href="asyncio-eventloops.html#asyncio.ProactorEventLoop" title="asyncio.ProactorEventLoop"><code class="xref py py-class docutils literal"><span class="pre">ProactorEventLoop</span></code></a>, this method is not supported.</p>
405
 
<p>See <a class="reference internal" href="asyncio-protocol.html#asyncio-udp-echo-client-protocol"><span>UDP echo client protocol</span></a> and
406
 
<a class="reference internal" href="asyncio-protocol.html#asyncio-udp-echo-server-protocol"><span>UDP echo server protocol</span></a> examples.</p>
407
 
</dd></dl>
408
 
 
409
 
<dl class="method">
410
 
<dt id="asyncio.BaseEventLoop.create_unix_connection">
411
 
<em class="property">coroutine </em><code class="descclassname">BaseEventLoop.</code><code class="descname">create_unix_connection</code><span class="sig-paren">(</span><em>protocol_factory</em>, <em>path</em>, <em>*</em>, <em>ssl=None</em>, <em>sock=None</em>, <em>server_hostname=None</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.create_unix_connection" title="Permalink to this definition">¶</a></dt>
412
 
<dd><p>Create UNIX connection: socket family <a class="reference internal" href="socket.html#socket.AF_UNIX" title="socket.AF_UNIX"><code class="xref py py-data docutils literal"><span class="pre">AF_UNIX</span></code></a>, socket
413
 
type <a class="reference internal" href="socket.html#socket.SOCK_STREAM" title="socket.SOCK_STREAM"><code class="xref py py-data docutils literal"><span class="pre">SOCK_STREAM</span></code></a>. The <a class="reference internal" href="socket.html#socket.AF_UNIX" title="socket.AF_UNIX"><code class="xref py py-data docutils literal"><span class="pre">AF_UNIX</span></code></a> socket
414
 
family is used to communicate between processes on the same machine
415
 
efficiently.</p>
416
 
<p>This method is a <a class="reference internal" href="asyncio-task.html#coroutine"><span>coroutine</span></a> which will try to
417
 
establish the connection in the background.  When successful, the
418
 
coroutine returns a <code class="docutils literal"><span class="pre">(transport,</span> <span class="pre">protocol)</span></code> pair.</p>
419
 
<p>See the <a class="reference internal" href="#asyncio.BaseEventLoop.create_connection" title="asyncio.BaseEventLoop.create_connection"><code class="xref py py-meth docutils literal"><span class="pre">BaseEventLoop.create_connection()</span></code></a> method for parameters.</p>
420
 
<p>Availability: UNIX.</p>
421
 
</dd></dl>
422
 
 
423
 
</div>
424
 
<div class="section" id="creating-listening-connections">
425
 
<h2>18.5.1.6. Creating listening connections<a class="headerlink" href="#creating-listening-connections" title="Permalink to this headline">¶</a></h2>
426
 
<dl class="method">
427
 
<dt id="asyncio.BaseEventLoop.create_server">
428
 
<em class="property">coroutine </em><code class="descclassname">BaseEventLoop.</code><code class="descname">create_server</code><span class="sig-paren">(</span><em>protocol_factory</em>, <em>host=None</em>, <em>port=None</em>, <em>*</em>, <em>family=socket.AF_UNSPEC</em>, <em>flags=socket.AI_PASSIVE</em>, <em>sock=None</em>, <em>backlog=100</em>, <em>ssl=None</em>, <em>reuse_address=None</em>, <em>reuse_port=None</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.create_server" title="Permalink to this definition">¶</a></dt>
429
 
<dd><p>Create a TCP server (socket type <a class="reference internal" href="socket.html#socket.SOCK_STREAM" title="socket.SOCK_STREAM"><code class="xref py py-data docutils literal"><span class="pre">SOCK_STREAM</span></code></a>) bound to
430
 
<em>host</em> and <em>port</em>.</p>
431
 
<p>Return a <a class="reference internal" href="#asyncio.Server" title="asyncio.Server"><code class="xref py py-class docutils literal"><span class="pre">Server</span></code></a> object, its <a class="reference internal" href="#asyncio.Server.sockets" title="asyncio.Server.sockets"><code class="xref py py-attr docutils literal"><span class="pre">sockets</span></code></a> attribute
432
 
contains created sockets. Use the <a class="reference internal" href="#asyncio.Server.close" title="asyncio.Server.close"><code class="xref py py-meth docutils literal"><span class="pre">Server.close()</span></code></a> method to stop the
433
 
server: close listening sockets.</p>
434
 
<p>Parameters:</p>
435
 
<ul class="simple">
436
 
<li>The <em>host</em> parameter can be a string, in that case the TCP server is
437
 
bound to <em>host</em> and <em>port</em>. The <em>host</em> parameter can also be a sequence
438
 
of strings and in that case the TCP server is bound to all hosts of the
439
 
sequence. If <em>host</em> is an empty string or <code class="docutils literal"><span class="pre">None</span></code>, all interfaces are
440
 
assumed and a list of multiple sockets will be returned (most likely one
441
 
for IPv4 and another one for IPv6).</li>
442
 
<li><em>family</em> can be set to either <a class="reference internal" href="socket.html#socket.AF_INET" title="socket.AF_INET"><code class="xref py py-data docutils literal"><span class="pre">socket.AF_INET</span></code></a> or
443
 
<a class="reference internal" href="socket.html#socket.AF_INET6" title="socket.AF_INET6"><code class="xref py py-data docutils literal"><span class="pre">AF_INET6</span></code></a> to force the socket to use IPv4 or IPv6. If not set
444
 
it will be determined from host (defaults to <code class="xref py py-data docutils literal"><span class="pre">socket.AF_UNSPEC</span></code>).</li>
445
 
<li><em>flags</em> is a bitmask for <a class="reference internal" href="#asyncio.BaseEventLoop.getaddrinfo" title="asyncio.BaseEventLoop.getaddrinfo"><code class="xref py py-meth docutils literal"><span class="pre">getaddrinfo()</span></code></a>.</li>
446
 
<li><em>sock</em> can optionally be specified in order to use a preexisting
447
 
socket object. If specified, <em>host</em> and <em>port</em> should be omitted (must be
448
 
<a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal"><span class="pre">None</span></code></a>).</li>
449
 
<li><em>backlog</em> is the maximum number of queued connections passed to
450
 
<a class="reference internal" href="socket.html#socket.socket.listen" title="socket.socket.listen"><code class="xref py py-meth docutils literal"><span class="pre">listen()</span></code></a> (defaults to 100).</li>
451
 
<li><em>ssl</em> can be set to an <a class="reference internal" href="ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal"><span class="pre">SSLContext</span></code></a> to enable SSL over the
452
 
accepted connections.</li>
453
 
<li><em>reuse_address</em> tells the kernel to reuse a local socket in
454
 
TIME_WAIT state, without waiting for its natural timeout to
455
 
expire. If not specified will automatically be set to True on
456
 
UNIX.</li>
457
 
<li><em>reuse_port</em> tells the kernel to allow this endpoint to be bound to the
458
 
same port as other existing endpoints are bound to, so long as they all
459
 
set this flag when being created. This option is not supported on
460
 
Windows.</li>
461
 
</ul>
462
 
<p>This method is a <a class="reference internal" href="asyncio-task.html#coroutine"><span>coroutine</span></a>.</p>
463
 
<div class="versionchanged">
464
 
<p><span class="versionmodified">Changed in version 3.5: </span>On Windows with <a class="reference internal" href="asyncio-eventloops.html#asyncio.ProactorEventLoop" title="asyncio.ProactorEventLoop"><code class="xref py py-class docutils literal"><span class="pre">ProactorEventLoop</span></code></a>, SSL/TLS is now supported.</p>
465
 
</div>
466
 
<div class="admonition seealso">
467
 
<p class="first admonition-title">See also</p>
468
 
<p class="last">The function <a class="reference internal" href="asyncio-stream.html#asyncio.start_server" title="asyncio.start_server"><code class="xref py py-func docutils literal"><span class="pre">start_server()</span></code></a> creates a (<a class="reference internal" href="asyncio-stream.html#asyncio.StreamReader" title="asyncio.StreamReader"><code class="xref py py-class docutils literal"><span class="pre">StreamReader</span></code></a>,
469
 
<a class="reference internal" href="asyncio-stream.html#asyncio.StreamWriter" title="asyncio.StreamWriter"><code class="xref py py-class docutils literal"><span class="pre">StreamWriter</span></code></a>) pair and calls back a function with this pair.</p>
470
 
</div>
471
 
<div class="versionchanged">
472
 
<p><span class="versionmodified">Changed in version 3.5.1: </span>The <em>host</em> parameter can now be a sequence of strings.</p>
473
 
</div>
474
 
</dd></dl>
475
 
 
476
 
<dl class="method">
477
 
<dt id="asyncio.BaseEventLoop.create_unix_server">
478
 
<em class="property">coroutine </em><code class="descclassname">BaseEventLoop.</code><code class="descname">create_unix_server</code><span class="sig-paren">(</span><em>protocol_factory</em>, <em>path=None</em>, <em>*</em>, <em>sock=None</em>, <em>backlog=100</em>, <em>ssl=None</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.create_unix_server" title="Permalink to this definition">¶</a></dt>
479
 
<dd><p>Similar to <a class="reference internal" href="#asyncio.BaseEventLoop.create_server" title="asyncio.BaseEventLoop.create_server"><code class="xref py py-meth docutils literal"><span class="pre">BaseEventLoop.create_server()</span></code></a>, but specific to the
480
 
socket family <a class="reference internal" href="socket.html#socket.AF_UNIX" title="socket.AF_UNIX"><code class="xref py py-data docutils literal"><span class="pre">AF_UNIX</span></code></a>.</p>
481
 
<p>This method is a <a class="reference internal" href="asyncio-task.html#coroutine"><span>coroutine</span></a>.</p>
482
 
<p>Availability: UNIX.</p>
483
 
</dd></dl>
484
 
 
485
 
</div>
486
 
<div class="section" id="watch-file-descriptors">
487
 
<h2>18.5.1.7. Watch file descriptors<a class="headerlink" href="#watch-file-descriptors" title="Permalink to this headline">¶</a></h2>
488
 
<p>On Windows with <a class="reference internal" href="asyncio-eventloops.html#asyncio.SelectorEventLoop" title="asyncio.SelectorEventLoop"><code class="xref py py-class docutils literal"><span class="pre">SelectorEventLoop</span></code></a>, only socket handles are supported
489
 
(ex: pipe file descriptors are not supported).</p>
490
 
<p>On Windows with <a class="reference internal" href="asyncio-eventloops.html#asyncio.ProactorEventLoop" title="asyncio.ProactorEventLoop"><code class="xref py py-class docutils literal"><span class="pre">ProactorEventLoop</span></code></a>, these methods are not supported.</p>
491
 
<dl class="method">
492
 
<dt id="asyncio.BaseEventLoop.add_reader">
493
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">add_reader</code><span class="sig-paren">(</span><em>fd</em>, <em>callback</em>, <em>*args</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.add_reader" title="Permalink to this definition">¶</a></dt>
494
 
<dd><p>Start watching the file descriptor for read availability and then call the
495
 
<em>callback</em> with specified arguments.</p>
496
 
<p><a class="reference internal" href="#asyncio-pass-keywords"><span>Use functools.partial to pass keywords to the callback</span></a>.</p>
497
 
</dd></dl>
498
 
 
499
 
<dl class="method">
500
 
<dt id="asyncio.BaseEventLoop.remove_reader">
501
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">remove_reader</code><span class="sig-paren">(</span><em>fd</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.remove_reader" title="Permalink to this definition">¶</a></dt>
502
 
<dd><p>Stop watching the file descriptor for read availability.</p>
503
 
</dd></dl>
504
 
 
505
 
<dl class="method">
506
 
<dt id="asyncio.BaseEventLoop.add_writer">
507
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">add_writer</code><span class="sig-paren">(</span><em>fd</em>, <em>callback</em>, <em>*args</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.add_writer" title="Permalink to this definition">¶</a></dt>
508
 
<dd><p>Start watching the file descriptor for write availability and then call the
509
 
<em>callback</em> with specified arguments.</p>
510
 
<p><a class="reference internal" href="#asyncio-pass-keywords"><span>Use functools.partial to pass keywords to the callback</span></a>.</p>
511
 
</dd></dl>
512
 
 
513
 
<dl class="method">
514
 
<dt id="asyncio.BaseEventLoop.remove_writer">
515
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">remove_writer</code><span class="sig-paren">(</span><em>fd</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.remove_writer" title="Permalink to this definition">¶</a></dt>
516
 
<dd><p>Stop watching the file descriptor for write availability.</p>
517
 
</dd></dl>
518
 
 
519
 
<p>The <a class="reference internal" href="#asyncio-watch-read-event"><span>watch a file descriptor for read events</span></a>
520
 
example uses the low-level <a class="reference internal" href="#asyncio.BaseEventLoop.add_reader" title="asyncio.BaseEventLoop.add_reader"><code class="xref py py-meth docutils literal"><span class="pre">BaseEventLoop.add_reader()</span></code></a> method to register
521
 
the file descriptor of a socket.</p>
522
 
</div>
523
 
<div class="section" id="low-level-socket-operations">
524
 
<h2>18.5.1.8. Low-level socket operations<a class="headerlink" href="#low-level-socket-operations" title="Permalink to this headline">¶</a></h2>
525
 
<dl class="method">
526
 
<dt id="asyncio.BaseEventLoop.sock_recv">
527
 
<em class="property">coroutine </em><code class="descclassname">BaseEventLoop.</code><code class="descname">sock_recv</code><span class="sig-paren">(</span><em>sock</em>, <em>nbytes</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.sock_recv" title="Permalink to this definition">¶</a></dt>
528
 
<dd><p>Receive data from the socket.  The return value is a bytes object
529
 
representing the data received.  The maximum amount of data to be received
530
 
at once is specified by <em>nbytes</em>.</p>
531
 
<p>With <a class="reference internal" href="asyncio-eventloops.html#asyncio.SelectorEventLoop" title="asyncio.SelectorEventLoop"><code class="xref py py-class docutils literal"><span class="pre">SelectorEventLoop</span></code></a> event loop, the socket <em>sock</em> must be
532
 
non-blocking.</p>
533
 
<p>This method is a <a class="reference internal" href="asyncio-task.html#coroutine"><span>coroutine</span></a>.</p>
534
 
<div class="admonition seealso">
535
 
<p class="first admonition-title">See also</p>
536
 
<p class="last">The <a class="reference internal" href="socket.html#socket.socket.recv" title="socket.socket.recv"><code class="xref py py-meth docutils literal"><span class="pre">socket.socket.recv()</span></code></a> method.</p>
537
 
</div>
538
 
</dd></dl>
539
 
 
540
 
<dl class="method">
541
 
<dt id="asyncio.BaseEventLoop.sock_sendall">
542
 
<em class="property">coroutine </em><code class="descclassname">BaseEventLoop.</code><code class="descname">sock_sendall</code><span class="sig-paren">(</span><em>sock</em>, <em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.sock_sendall" title="Permalink to this definition">¶</a></dt>
543
 
<dd><p>Send data to the socket.  The socket must be connected to a remote socket.
544
 
This method continues to send data from <em>data</em> until either all data has
545
 
been sent or an error occurs.  <code class="docutils literal"><span class="pre">None</span></code> is returned on success.  On error,
546
 
an exception is raised, and there is no way to determine how much data, if
547
 
any, was successfully processed by the receiving end of the connection.</p>
548
 
<p>With <a class="reference internal" href="asyncio-eventloops.html#asyncio.SelectorEventLoop" title="asyncio.SelectorEventLoop"><code class="xref py py-class docutils literal"><span class="pre">SelectorEventLoop</span></code></a> event loop, the socket <em>sock</em> must be
549
 
non-blocking.</p>
550
 
<p>This method is a <a class="reference internal" href="asyncio-task.html#coroutine"><span>coroutine</span></a>.</p>
551
 
<div class="admonition seealso">
552
 
<p class="first admonition-title">See also</p>
553
 
<p class="last">The <a class="reference internal" href="socket.html#socket.socket.sendall" title="socket.socket.sendall"><code class="xref py py-meth docutils literal"><span class="pre">socket.socket.sendall()</span></code></a> method.</p>
554
 
</div>
555
 
</dd></dl>
556
 
 
557
 
<dl class="method">
558
 
<dt id="asyncio.BaseEventLoop.sock_connect">
559
 
<em class="property">coroutine </em><code class="descclassname">BaseEventLoop.</code><code class="descname">sock_connect</code><span class="sig-paren">(</span><em>sock</em>, <em>address</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.sock_connect" title="Permalink to this definition">¶</a></dt>
560
 
<dd><p>Connect to a remote socket at <em>address</em>.</p>
561
 
<p>The <em>address</em> must be already resolved to avoid the trap of hanging the
562
 
entire event loop when the address requires doing a DNS lookup.  For
563
 
example, it must be an IP address, not an hostname, for
564
 
<a class="reference internal" href="socket.html#socket.AF_INET" title="socket.AF_INET"><code class="xref py py-data docutils literal"><span class="pre">AF_INET</span></code></a> and <a class="reference internal" href="socket.html#socket.AF_INET6" title="socket.AF_INET6"><code class="xref py py-data docutils literal"><span class="pre">AF_INET6</span></code></a> address families.
565
 
Use <a class="reference internal" href="#asyncio.BaseEventLoop.getaddrinfo" title="asyncio.BaseEventLoop.getaddrinfo"><code class="xref py py-meth docutils literal"><span class="pre">getaddrinfo()</span></code></a> to resolve the hostname asynchronously.</p>
566
 
<p>With <a class="reference internal" href="asyncio-eventloops.html#asyncio.SelectorEventLoop" title="asyncio.SelectorEventLoop"><code class="xref py py-class docutils literal"><span class="pre">SelectorEventLoop</span></code></a> event loop, the socket <em>sock</em> must be
567
 
non-blocking.</p>
568
 
<p>This method is a <a class="reference internal" href="asyncio-task.html#coroutine"><span>coroutine</span></a>.</p>
569
 
<div class="admonition seealso">
570
 
<p class="first admonition-title">See also</p>
571
 
<p class="last">The <a class="reference internal" href="#asyncio.BaseEventLoop.create_connection" title="asyncio.BaseEventLoop.create_connection"><code class="xref py py-meth docutils literal"><span class="pre">BaseEventLoop.create_connection()</span></code></a> method, the
572
 
<a class="reference internal" href="asyncio-stream.html#asyncio.open_connection" title="asyncio.open_connection"><code class="xref py py-func docutils literal"><span class="pre">open_connection()</span></code></a> function and the <a class="reference internal" href="socket.html#socket.socket.connect" title="socket.socket.connect"><code class="xref py py-meth docutils literal"><span class="pre">socket.socket.connect()</span></code></a>
573
 
method.</p>
574
 
</div>
575
 
</dd></dl>
576
 
 
577
 
<dl class="method">
578
 
<dt id="asyncio.BaseEventLoop.sock_accept">
579
 
<em class="property">coroutine </em><code class="descclassname">BaseEventLoop.</code><code class="descname">sock_accept</code><span class="sig-paren">(</span><em>sock</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.sock_accept" title="Permalink to this definition">¶</a></dt>
580
 
<dd><p>Accept a connection. The socket must be bound to an address and listening
581
 
for connections. The return value is a pair <code class="docutils literal"><span class="pre">(conn,</span> <span class="pre">address)</span></code> where <em>conn</em>
582
 
is a <em>new</em> socket object usable to send and receive data on the connection,
583
 
and <em>address</em> is the address bound to the socket on the other end of the
584
 
connection.</p>
585
 
<p>The socket <em>sock</em> must be non-blocking.</p>
586
 
<p>This method is a <a class="reference internal" href="asyncio-task.html#coroutine"><span>coroutine</span></a>.</p>
587
 
<div class="admonition seealso">
588
 
<p class="first admonition-title">See also</p>
589
 
<p class="last">The <a class="reference internal" href="#asyncio.BaseEventLoop.create_server" title="asyncio.BaseEventLoop.create_server"><code class="xref py py-meth docutils literal"><span class="pre">BaseEventLoop.create_server()</span></code></a> method, the <a class="reference internal" href="asyncio-stream.html#asyncio.start_server" title="asyncio.start_server"><code class="xref py py-func docutils literal"><span class="pre">start_server()</span></code></a>
590
 
function and the <a class="reference internal" href="socket.html#socket.socket.accept" title="socket.socket.accept"><code class="xref py py-meth docutils literal"><span class="pre">socket.socket.accept()</span></code></a> method.</p>
591
 
</div>
592
 
</dd></dl>
593
 
 
594
 
</div>
595
 
<div class="section" id="resolve-host-name">
596
 
<h2>18.5.1.9. Resolve host name<a class="headerlink" href="#resolve-host-name" title="Permalink to this headline">¶</a></h2>
597
 
<dl class="method">
598
 
<dt id="asyncio.BaseEventLoop.getaddrinfo">
599
 
<em class="property">coroutine </em><code class="descclassname">BaseEventLoop.</code><code class="descname">getaddrinfo</code><span class="sig-paren">(</span><em>host</em>, <em>port</em>, <em>*</em>, <em>family=0</em>, <em>type=0</em>, <em>proto=0</em>, <em>flags=0</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.getaddrinfo" title="Permalink to this definition">¶</a></dt>
600
 
<dd><p>This method is a <a class="reference internal" href="asyncio-task.html#coroutine"><span>coroutine</span></a>, similar to
601
 
<a class="reference internal" href="socket.html#socket.getaddrinfo" title="socket.getaddrinfo"><code class="xref py py-meth docutils literal"><span class="pre">socket.getaddrinfo()</span></code></a> function but non-blocking.</p>
602
 
</dd></dl>
603
 
 
604
 
<dl class="method">
605
 
<dt id="asyncio.BaseEventLoop.getnameinfo">
606
 
<em class="property">coroutine </em><code class="descclassname">BaseEventLoop.</code><code class="descname">getnameinfo</code><span class="sig-paren">(</span><em>sockaddr</em>, <em>flags=0</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.getnameinfo" title="Permalink to this definition">¶</a></dt>
607
 
<dd><p>This method is a <a class="reference internal" href="asyncio-task.html#coroutine"><span>coroutine</span></a>, similar to
608
 
<a class="reference internal" href="socket.html#socket.getnameinfo" title="socket.getnameinfo"><code class="xref py py-meth docutils literal"><span class="pre">socket.getnameinfo()</span></code></a> function but non-blocking.</p>
609
 
</dd></dl>
610
 
 
611
 
</div>
612
 
<div class="section" id="connect-pipes">
613
 
<h2>18.5.1.10. Connect pipes<a class="headerlink" href="#connect-pipes" title="Permalink to this headline">¶</a></h2>
614
 
<p>On Windows with <a class="reference internal" href="asyncio-eventloops.html#asyncio.SelectorEventLoop" title="asyncio.SelectorEventLoop"><code class="xref py py-class docutils literal"><span class="pre">SelectorEventLoop</span></code></a>, these methods are not supported.
615
 
Use <a class="reference internal" href="asyncio-eventloops.html#asyncio.ProactorEventLoop" title="asyncio.ProactorEventLoop"><code class="xref py py-class docutils literal"><span class="pre">ProactorEventLoop</span></code></a> to support pipes on Windows.</p>
616
 
<dl class="method">
617
 
<dt id="asyncio.BaseEventLoop.connect_read_pipe">
618
 
<em class="property">coroutine </em><code class="descclassname">BaseEventLoop.</code><code class="descname">connect_read_pipe</code><span class="sig-paren">(</span><em>protocol_factory</em>, <em>pipe</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.connect_read_pipe" title="Permalink to this definition">¶</a></dt>
619
 
<dd><p>Register read pipe in eventloop.</p>
620
 
<p><em>protocol_factory</em> should instantiate object with <a class="reference internal" href="asyncio-protocol.html#asyncio.Protocol" title="asyncio.Protocol"><code class="xref py py-class docutils literal"><span class="pre">Protocol</span></code></a>
621
 
interface.  <em>pipe</em> is a <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file-like object</span></a>.
622
 
Return pair <code class="docutils literal"><span class="pre">(transport,</span> <span class="pre">protocol)</span></code>, where <em>transport</em> supports the
623
 
<a class="reference internal" href="asyncio-protocol.html#asyncio.ReadTransport" title="asyncio.ReadTransport"><code class="xref py py-class docutils literal"><span class="pre">ReadTransport</span></code></a> interface.</p>
624
 
<p>With <a class="reference internal" href="asyncio-eventloops.html#asyncio.SelectorEventLoop" title="asyncio.SelectorEventLoop"><code class="xref py py-class docutils literal"><span class="pre">SelectorEventLoop</span></code></a> event loop, the <em>pipe</em> is set to
625
 
non-blocking mode.</p>
626
 
<p>This method is a <a class="reference internal" href="asyncio-task.html#coroutine"><span>coroutine</span></a>.</p>
627
 
</dd></dl>
628
 
 
629
 
<dl class="method">
630
 
<dt id="asyncio.BaseEventLoop.connect_write_pipe">
631
 
<em class="property">coroutine </em><code class="descclassname">BaseEventLoop.</code><code class="descname">connect_write_pipe</code><span class="sig-paren">(</span><em>protocol_factory</em>, <em>pipe</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.connect_write_pipe" title="Permalink to this definition">¶</a></dt>
632
 
<dd><p>Register write pipe in eventloop.</p>
633
 
<p><em>protocol_factory</em> should instantiate object with <code class="xref py py-class docutils literal"><span class="pre">BaseProtocol</span></code>
634
 
interface. <em>pipe</em> is <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file-like object</span></a>.
635
 
Return pair <code class="docutils literal"><span class="pre">(transport,</span> <span class="pre">protocol)</span></code>, where <em>transport</em> supports
636
 
<a class="reference internal" href="asyncio-protocol.html#asyncio.WriteTransport" title="asyncio.WriteTransport"><code class="xref py py-class docutils literal"><span class="pre">WriteTransport</span></code></a> interface.</p>
637
 
<p>With <a class="reference internal" href="asyncio-eventloops.html#asyncio.SelectorEventLoop" title="asyncio.SelectorEventLoop"><code class="xref py py-class docutils literal"><span class="pre">SelectorEventLoop</span></code></a> event loop, the <em>pipe</em> is set to
638
 
non-blocking mode.</p>
639
 
<p>This method is a <a class="reference internal" href="asyncio-task.html#coroutine"><span>coroutine</span></a>.</p>
640
 
</dd></dl>
641
 
 
642
 
<div class="admonition seealso">
643
 
<p class="first admonition-title">See also</p>
644
 
<p class="last">The <a class="reference internal" href="asyncio-subprocess.html#asyncio.BaseEventLoop.subprocess_exec" title="asyncio.BaseEventLoop.subprocess_exec"><code class="xref py py-meth docutils literal"><span class="pre">BaseEventLoop.subprocess_exec()</span></code></a> and
645
 
<a class="reference internal" href="asyncio-subprocess.html#asyncio.BaseEventLoop.subprocess_shell" title="asyncio.BaseEventLoop.subprocess_shell"><code class="xref py py-meth docutils literal"><span class="pre">BaseEventLoop.subprocess_shell()</span></code></a> methods.</p>
646
 
</div>
647
 
</div>
648
 
<div class="section" id="unix-signals">
649
 
<h2>18.5.1.11. UNIX signals<a class="headerlink" href="#unix-signals" title="Permalink to this headline">¶</a></h2>
650
 
<p>Availability: UNIX only.</p>
651
 
<dl class="method">
652
 
<dt id="asyncio.BaseEventLoop.add_signal_handler">
653
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">add_signal_handler</code><span class="sig-paren">(</span><em>signum</em>, <em>callback</em>, <em>*args</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.add_signal_handler" title="Permalink to this definition">¶</a></dt>
654
 
<dd><p>Add a handler for a signal.</p>
655
 
<p>Raise <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> if the signal number is invalid or uncatchable.
656
 
Raise <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal"><span class="pre">RuntimeError</span></code></a> if there is a problem setting up the handler.</p>
657
 
<p><a class="reference internal" href="#asyncio-pass-keywords"><span>Use functools.partial to pass keywords to the callback</span></a>.</p>
658
 
</dd></dl>
659
 
 
660
 
<dl class="method">
661
 
<dt id="asyncio.BaseEventLoop.remove_signal_handler">
662
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">remove_signal_handler</code><span class="sig-paren">(</span><em>sig</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.remove_signal_handler" title="Permalink to this definition">¶</a></dt>
663
 
<dd><p>Remove a handler for a signal.</p>
664
 
<p>Return <code class="docutils literal"><span class="pre">True</span></code> if a signal handler was removed, <code class="docutils literal"><span class="pre">False</span></code> if not.</p>
665
 
</dd></dl>
666
 
 
667
 
<div class="admonition seealso">
668
 
<p class="first admonition-title">See also</p>
669
 
<p class="last">The <a class="reference internal" href="signal.html#module-signal" title="signal: Set handlers for asynchronous events."><code class="xref py py-mod docutils literal"><span class="pre">signal</span></code></a> module.</p>
670
 
</div>
671
 
</div>
672
 
<div class="section" id="executor">
673
 
<h2>18.5.1.12. Executor<a class="headerlink" href="#executor" title="Permalink to this headline">¶</a></h2>
674
 
<p>Call a function in an <a class="reference internal" href="concurrent.futures.html#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal"><span class="pre">Executor</span></code></a> (pool of threads or
675
 
pool of processes). By default, an event loop uses a thread pool executor
676
 
(<a class="reference internal" href="concurrent.futures.html#concurrent.futures.ThreadPoolExecutor" title="concurrent.futures.ThreadPoolExecutor"><code class="xref py py-class docutils literal"><span class="pre">ThreadPoolExecutor</span></code></a>).</p>
677
 
<dl class="method">
678
 
<dt id="asyncio.BaseEventLoop.run_in_executor">
679
 
<em class="property">coroutine </em><code class="descclassname">BaseEventLoop.</code><code class="descname">run_in_executor</code><span class="sig-paren">(</span><em>executor</em>, <em>func</em>, <em>*args</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.run_in_executor" title="Permalink to this definition">¶</a></dt>
680
 
<dd><p>Arrange for a <em>func</em> to be called in the specified executor.</p>
681
 
<p>The <em>executor</em> argument should be an <a class="reference internal" href="concurrent.futures.html#concurrent.futures.Executor" title="concurrent.futures.Executor"><code class="xref py py-class docutils literal"><span class="pre">Executor</span></code></a>
682
 
instance. The default executor is used if <em>executor</em> is <code class="docutils literal"><span class="pre">None</span></code>.</p>
683
 
<p><a class="reference internal" href="#asyncio-pass-keywords"><span>Use functools.partial to pass keywords to the *func*</span></a>.</p>
684
 
<p>This method is a <a class="reference internal" href="asyncio-task.html#coroutine"><span>coroutine</span></a>.</p>
685
 
</dd></dl>
686
 
 
687
 
<dl class="method">
688
 
<dt id="asyncio.BaseEventLoop.set_default_executor">
689
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">set_default_executor</code><span class="sig-paren">(</span><em>executor</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.set_default_executor" title="Permalink to this definition">¶</a></dt>
690
 
<dd><p>Set the default executor used by <a class="reference internal" href="#asyncio.BaseEventLoop.run_in_executor" title="asyncio.BaseEventLoop.run_in_executor"><code class="xref py py-meth docutils literal"><span class="pre">run_in_executor()</span></code></a>.</p>
691
 
</dd></dl>
692
 
 
693
 
</div>
694
 
<div class="section" id="error-handling-api">
695
 
<h2>18.5.1.13. Error Handling API<a class="headerlink" href="#error-handling-api" title="Permalink to this headline">¶</a></h2>
696
 
<p>Allows customizing how exceptions are handled in the event loop.</p>
697
 
<dl class="method">
698
 
<dt id="asyncio.BaseEventLoop.set_exception_handler">
699
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">set_exception_handler</code><span class="sig-paren">(</span><em>handler</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.set_exception_handler" title="Permalink to this definition">¶</a></dt>
700
 
<dd><p>Set <em>handler</em> as the new event loop exception handler.</p>
701
 
<p>If <em>handler</em> is <code class="docutils literal"><span class="pre">None</span></code>, the default exception handler will
702
 
be set.</p>
703
 
<p>If <em>handler</em> is a callable object, it should have a
704
 
matching signature to <code class="docutils literal"><span class="pre">(loop,</span> <span class="pre">context)</span></code>, where <code class="docutils literal"><span class="pre">loop</span></code>
705
 
will be a reference to the active event loop, <code class="docutils literal"><span class="pre">context</span></code>
706
 
will be a <code class="docutils literal"><span class="pre">dict</span></code> object (see <a class="reference internal" href="#asyncio.BaseEventLoop.call_exception_handler" title="asyncio.BaseEventLoop.call_exception_handler"><code class="xref py py-meth docutils literal"><span class="pre">call_exception_handler()</span></code></a>
707
 
documentation for details about context).</p>
708
 
</dd></dl>
709
 
 
710
 
<dl class="method">
711
 
<dt id="asyncio.BaseEventLoop.default_exception_handler">
712
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">default_exception_handler</code><span class="sig-paren">(</span><em>context</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.default_exception_handler" title="Permalink to this definition">¶</a></dt>
713
 
<dd><p>Default exception handler.</p>
714
 
<p>This is called when an exception occurs and no exception
715
 
handler is set, and can be called by a custom exception
716
 
handler that wants to defer to the default behavior.</p>
717
 
<p><em>context</em> parameter has the same meaning as in
718
 
<a class="reference internal" href="#asyncio.BaseEventLoop.call_exception_handler" title="asyncio.BaseEventLoop.call_exception_handler"><code class="xref py py-meth docutils literal"><span class="pre">call_exception_handler()</span></code></a>.</p>
719
 
</dd></dl>
720
 
 
721
 
<dl class="method">
722
 
<dt id="asyncio.BaseEventLoop.call_exception_handler">
723
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">call_exception_handler</code><span class="sig-paren">(</span><em>context</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.call_exception_handler" title="Permalink to this definition">¶</a></dt>
724
 
<dd><p>Call the current event loop exception handler.</p>
725
 
<p><em>context</em> is a <code class="docutils literal"><span class="pre">dict</span></code> object containing the following keys
726
 
(new keys may be introduced later):</p>
727
 
<ul class="simple">
728
 
<li>&#8216;message&#8217;: Error message;</li>
729
 
<li>&#8216;exception&#8217; (optional): Exception object;</li>
730
 
<li>&#8216;future&#8217; (optional): <a class="reference internal" href="asyncio-task.html#asyncio.Future" title="asyncio.Future"><code class="xref py py-class docutils literal"><span class="pre">asyncio.Future</span></code></a> instance;</li>
731
 
<li>&#8216;handle&#8217; (optional): <a class="reference internal" href="#asyncio.Handle" title="asyncio.Handle"><code class="xref py py-class docutils literal"><span class="pre">asyncio.Handle</span></code></a> instance;</li>
732
 
<li>&#8216;protocol&#8217; (optional): <a class="reference internal" href="asyncio-protocol.html#asyncio-protocol"><span>Protocol</span></a> instance;</li>
733
 
<li>&#8216;transport&#8217; (optional): <a class="reference internal" href="asyncio-protocol.html#asyncio-transport"><span>Transport</span></a> instance;</li>
734
 
<li>&#8216;socket&#8217; (optional): <a class="reference internal" href="socket.html#socket.socket" title="socket.socket"><code class="xref py py-class docutils literal"><span class="pre">socket.socket</span></code></a> instance.</li>
735
 
</ul>
736
 
<div class="admonition note">
737
 
<p class="first admonition-title">Note</p>
738
 
<p class="last">Note: this method should not be overloaded in subclassed
739
 
event loops.  For any custom exception handling, use
740
 
<a class="reference internal" href="#asyncio.BaseEventLoop.set_exception_handler" title="asyncio.BaseEventLoop.set_exception_handler"><code class="xref py py-meth docutils literal"><span class="pre">set_exception_handler()</span></code></a> method.</p>
741
 
</div>
742
 
</dd></dl>
743
 
 
744
 
</div>
745
 
<div class="section" id="debug-mode">
746
 
<h2>18.5.1.14. Debug mode<a class="headerlink" href="#debug-mode" title="Permalink to this headline">¶</a></h2>
747
 
<dl class="method">
748
 
<dt id="asyncio.BaseEventLoop.get_debug">
749
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">get_debug</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.get_debug" title="Permalink to this definition">¶</a></dt>
750
 
<dd><p>Get the debug mode (<a class="reference internal" href="functions.html#bool" title="bool"><code class="xref py py-class docutils literal"><span class="pre">bool</span></code></a>) of the event loop.</p>
751
 
<p>The default value is <code class="docutils literal"><span class="pre">True</span></code> if the environment variable
752
 
<span class="target" id="index-0"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONASYNCIODEBUG"><code class="xref std std-envvar docutils literal"><span class="pre">PYTHONASYNCIODEBUG</span></code></a> is set to a non-empty string, <code class="docutils literal"><span class="pre">False</span></code>
753
 
otherwise.</p>
754
 
<div class="versionadded">
755
 
<p><span class="versionmodified">New in version 3.4.2.</span></p>
756
 
</div>
757
 
</dd></dl>
758
 
 
759
 
<dl class="method">
760
 
<dt id="asyncio.BaseEventLoop.set_debug">
761
 
<code class="descclassname">BaseEventLoop.</code><code class="descname">set_debug</code><span class="sig-paren">(</span><em>enabled: bool</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.BaseEventLoop.set_debug" title="Permalink to this definition">¶</a></dt>
762
 
<dd><p>Set the debug mode of the event loop.</p>
763
 
<div class="versionadded">
764
 
<p><span class="versionmodified">New in version 3.4.2.</span></p>
765
 
</div>
766
 
</dd></dl>
767
 
 
768
 
<div class="admonition seealso">
769
 
<p class="first admonition-title">See also</p>
770
 
<p class="last">The <a class="reference internal" href="asyncio-dev.html#asyncio-debug-mode"><span>debug mode of asyncio</span></a>.</p>
771
 
</div>
772
 
</div>
773
 
<div class="section" id="server">
774
 
<h2>18.5.1.15. Server<a class="headerlink" href="#server" title="Permalink to this headline">¶</a></h2>
775
 
<dl class="class">
776
 
<dt id="asyncio.Server">
777
 
<em class="property">class </em><code class="descclassname">asyncio.</code><code class="descname">Server</code><a class="headerlink" href="#asyncio.Server" title="Permalink to this definition">¶</a></dt>
778
 
<dd><p>Server listening on sockets.</p>
779
 
<p>Object created by the <a class="reference internal" href="#asyncio.BaseEventLoop.create_server" title="asyncio.BaseEventLoop.create_server"><code class="xref py py-meth docutils literal"><span class="pre">BaseEventLoop.create_server()</span></code></a> method and the
780
 
<a class="reference internal" href="asyncio-stream.html#asyncio.start_server" title="asyncio.start_server"><code class="xref py py-func docutils literal"><span class="pre">start_server()</span></code></a> function. Don&#8217;t instantiate the class directly.</p>
781
 
<dl class="method">
782
 
<dt id="asyncio.Server.close">
783
 
<code class="descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Server.close" title="Permalink to this definition">¶</a></dt>
784
 
<dd><p>Stop serving: close listening sockets and set the <a class="reference internal" href="#asyncio.Server.sockets" title="asyncio.Server.sockets"><code class="xref py py-attr docutils literal"><span class="pre">sockets</span></code></a>
785
 
attribute to <code class="docutils literal"><span class="pre">None</span></code>.</p>
786
 
<p>The sockets that represent existing incoming client connections are left
787
 
open.</p>
788
 
<p>The server is closed asynchronously, use the <a class="reference internal" href="#asyncio.Server.wait_closed" title="asyncio.Server.wait_closed"><code class="xref py py-meth docutils literal"><span class="pre">wait_closed()</span></code></a>
789
 
coroutine to wait until the server is closed.</p>
790
 
</dd></dl>
791
 
 
792
 
<dl class="method">
793
 
<dt id="asyncio.Server.wait_closed">
794
 
<em class="property">coroutine </em><code class="descname">wait_closed</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Server.wait_closed" title="Permalink to this definition">¶</a></dt>
795
 
<dd><p>Wait until the <a class="reference internal" href="#asyncio.Server.close" title="asyncio.Server.close"><code class="xref py py-meth docutils literal"><span class="pre">close()</span></code></a> method completes.</p>
796
 
<p>This method is a <a class="reference internal" href="asyncio-task.html#coroutine"><span>coroutine</span></a>.</p>
797
 
</dd></dl>
798
 
 
799
 
<dl class="attribute">
800
 
<dt id="asyncio.Server.sockets">
801
 
<code class="descname">sockets</code><a class="headerlink" href="#asyncio.Server.sockets" title="Permalink to this definition">¶</a></dt>
802
 
<dd><p>List of <a class="reference internal" href="socket.html#socket.socket" title="socket.socket"><code class="xref py py-class docutils literal"><span class="pre">socket.socket</span></code></a> objects the server is listening to, or
803
 
<code class="docutils literal"><span class="pre">None</span></code> if the server is closed.</p>
804
 
</dd></dl>
805
 
 
806
 
</dd></dl>
807
 
 
808
 
</div>
809
 
<div class="section" id="handle">
810
 
<h2>18.5.1.16. Handle<a class="headerlink" href="#handle" title="Permalink to this headline">¶</a></h2>
811
 
<dl class="class">
812
 
<dt id="asyncio.Handle">
813
 
<em class="property">class </em><code class="descclassname">asyncio.</code><code class="descname">Handle</code><a class="headerlink" href="#asyncio.Handle" title="Permalink to this definition">¶</a></dt>
814
 
<dd><p>A callback wrapper object returned by <a class="reference internal" href="#asyncio.BaseEventLoop.call_soon" title="asyncio.BaseEventLoop.call_soon"><code class="xref py py-func docutils literal"><span class="pre">BaseEventLoop.call_soon()</span></code></a>,
815
 
<a class="reference internal" href="#asyncio.BaseEventLoop.call_soon_threadsafe" title="asyncio.BaseEventLoop.call_soon_threadsafe"><code class="xref py py-func docutils literal"><span class="pre">BaseEventLoop.call_soon_threadsafe()</span></code></a>, <a class="reference internal" href="#asyncio.BaseEventLoop.call_later" title="asyncio.BaseEventLoop.call_later"><code class="xref py py-func docutils literal"><span class="pre">BaseEventLoop.call_later()</span></code></a>,
816
 
and <a class="reference internal" href="#asyncio.BaseEventLoop.call_at" title="asyncio.BaseEventLoop.call_at"><code class="xref py py-func docutils literal"><span class="pre">BaseEventLoop.call_at()</span></code></a>.</p>
817
 
<dl class="method">
818
 
<dt id="asyncio.Handle.cancel">
819
 
<code class="descname">cancel</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.Handle.cancel" title="Permalink to this definition">¶</a></dt>
820
 
<dd><p>Cancel the call.  If the callback is already canceled or executed,
821
 
this method has no effect.</p>
822
 
</dd></dl>
823
 
 
824
 
</dd></dl>
825
 
 
826
 
</div>
827
 
<div class="section" id="event-loop-examples">
828
 
<h2>18.5.1.17. Event loop examples<a class="headerlink" href="#event-loop-examples" title="Permalink to this headline">¶</a></h2>
829
 
<div class="section" id="hello-world-with-call-soon">
830
 
<span id="asyncio-hello-world-callback"></span><h3>18.5.1.17.1. Hello World with call_soon()<a class="headerlink" href="#hello-world-with-call-soon" title="Permalink to this headline">¶</a></h3>
831
 
<p>Example using the <a class="reference internal" href="#asyncio.BaseEventLoop.call_soon" title="asyncio.BaseEventLoop.call_soon"><code class="xref py py-meth docutils literal"><span class="pre">BaseEventLoop.call_soon()</span></code></a> method to schedule a
832
 
callback. The callback displays <code class="docutils literal"><span class="pre">&quot;Hello</span> <span class="pre">World&quot;</span></code> and then stops the event
833
 
loop:</p>
834
 
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span>
835
 
 
836
 
<span class="k">def</span> <span class="nf">hello_world</span><span class="p">(</span><span class="n">loop</span><span class="p">):</span>
837
 
    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Hello World&#39;</span><span class="p">)</span>
838
 
    <span class="n">loop</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
839
 
 
840
 
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
841
 
 
842
 
<span class="c1"># Schedule a call to hello_world()</span>
843
 
<span class="n">loop</span><span class="o">.</span><span class="n">call_soon</span><span class="p">(</span><span class="n">hello_world</span><span class="p">,</span> <span class="n">loop</span><span class="p">)</span>
844
 
 
845
 
<span class="c1"># Blocking call interrupted by loop.stop()</span>
846
 
<span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
847
 
<span class="n">loop</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
848
 
</pre></div>
849
 
</div>
850
 
<div class="admonition seealso">
851
 
<p class="first admonition-title">See also</p>
852
 
<p class="last">The <a class="reference internal" href="asyncio-task.html#asyncio-hello-world-coroutine"><span>Hello World coroutine</span></a> example
853
 
uses a <a class="reference internal" href="asyncio-task.html#coroutine"><span>coroutine</span></a>.</p>
854
 
</div>
855
 
</div>
856
 
<div class="section" id="display-the-current-date-with-call-later">
857
 
<span id="asyncio-date-callback"></span><h3>18.5.1.17.2. Display the current date with call_later()<a class="headerlink" href="#display-the-current-date-with-call-later" title="Permalink to this headline">¶</a></h3>
858
 
<p>Example of callback displaying the current date every second. The callback uses
859
 
the <a class="reference internal" href="#asyncio.BaseEventLoop.call_later" title="asyncio.BaseEventLoop.call_later"><code class="xref py py-meth docutils literal"><span class="pre">BaseEventLoop.call_later()</span></code></a> method to reschedule itself during 5
860
 
seconds, and then stops the event loop:</p>
861
 
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span>
862
 
<span class="kn">import</span> <span class="nn">datetime</span>
863
 
 
864
 
<span class="k">def</span> <span class="nf">display_date</span><span class="p">(</span><span class="n">end_time</span><span class="p">,</span> <span class="n">loop</span><span class="p">):</span>
865
 
    <span class="nb">print</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">())</span>
866
 
    <span class="k">if</span> <span class="p">(</span><span class="n">loop</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="mf">1.0</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">end_time</span><span class="p">:</span>
867
 
        <span class="n">loop</span><span class="o">.</span><span class="n">call_later</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">display_date</span><span class="p">,</span> <span class="n">end_time</span><span class="p">,</span> <span class="n">loop</span><span class="p">)</span>
868
 
    <span class="k">else</span><span class="p">:</span>
869
 
        <span class="n">loop</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
870
 
 
871
 
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
872
 
 
873
 
<span class="c1"># Schedule the first call to display_date()</span>
874
 
<span class="n">end_time</span> <span class="o">=</span> <span class="n">loop</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="mf">5.0</span>
875
 
<span class="n">loop</span><span class="o">.</span><span class="n">call_soon</span><span class="p">(</span><span class="n">display_date</span><span class="p">,</span> <span class="n">end_time</span><span class="p">,</span> <span class="n">loop</span><span class="p">)</span>
876
 
 
877
 
<span class="c1"># Blocking call interrupted by loop.stop()</span>
878
 
<span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
879
 
<span class="n">loop</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
880
 
</pre></div>
881
 
</div>
882
 
<div class="admonition seealso">
883
 
<p class="first admonition-title">See also</p>
884
 
<p class="last">The <a class="reference internal" href="asyncio-task.html#asyncio-date-coroutine"><span>coroutine displaying the current date</span></a> example uses a <a class="reference internal" href="asyncio-task.html#coroutine"><span>coroutine</span></a>.</p>
885
 
</div>
886
 
</div>
887
 
<div class="section" id="watch-a-file-descriptor-for-read-events">
888
 
<span id="asyncio-watch-read-event"></span><h3>18.5.1.17.3. Watch a file descriptor for read events<a class="headerlink" href="#watch-a-file-descriptor-for-read-events" title="Permalink to this headline">¶</a></h3>
889
 
<p>Wait until a file descriptor received some data using the
890
 
<a class="reference internal" href="#asyncio.BaseEventLoop.add_reader" title="asyncio.BaseEventLoop.add_reader"><code class="xref py py-meth docutils literal"><span class="pre">BaseEventLoop.add_reader()</span></code></a> method and then close the event loop:</p>
891
 
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span>
892
 
<span class="k">try</span><span class="p">:</span>
893
 
    <span class="kn">from</span> <span class="nn">socket</span> <span class="k">import</span> <span class="n">socketpair</span>
894
 
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
895
 
    <span class="kn">from</span> <span class="nn">asyncio.windows_utils</span> <span class="k">import</span> <span class="n">socketpair</span>
896
 
 
897
 
<span class="c1"># Create a pair of connected file descriptors</span>
898
 
<span class="n">rsock</span><span class="p">,</span> <span class="n">wsock</span> <span class="o">=</span> <span class="n">socketpair</span><span class="p">()</span>
899
 
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
900
 
 
901
 
<span class="k">def</span> <span class="nf">reader</span><span class="p">():</span>
902
 
    <span class="n">data</span> <span class="o">=</span> <span class="n">rsock</span><span class="o">.</span><span class="n">recv</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span>
903
 
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Received:&quot;</span><span class="p">,</span> <span class="n">data</span><span class="o">.</span><span class="n">decode</span><span class="p">())</span>
904
 
    <span class="c1"># We are done: unregister the file descriptor</span>
905
 
    <span class="n">loop</span><span class="o">.</span><span class="n">remove_reader</span><span class="p">(</span><span class="n">rsock</span><span class="p">)</span>
906
 
    <span class="c1"># Stop the event loop</span>
907
 
    <span class="n">loop</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
908
 
 
909
 
<span class="c1"># Register the file descriptor for read event</span>
910
 
<span class="n">loop</span><span class="o">.</span><span class="n">add_reader</span><span class="p">(</span><span class="n">rsock</span><span class="p">,</span> <span class="n">reader</span><span class="p">)</span>
911
 
 
912
 
<span class="c1"># Simulate the reception of data from the network</span>
913
 
<span class="n">loop</span><span class="o">.</span><span class="n">call_soon</span><span class="p">(</span><span class="n">wsock</span><span class="o">.</span><span class="n">send</span><span class="p">,</span> <span class="s1">&#39;abc&#39;</span><span class="o">.</span><span class="n">encode</span><span class="p">())</span>
914
 
 
915
 
<span class="c1"># Run the event loop</span>
916
 
<span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
917
 
 
918
 
<span class="c1"># We are done, close sockets and the event loop</span>
919
 
<span class="n">rsock</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
920
 
<span class="n">wsock</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
921
 
<span class="n">loop</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
922
 
</pre></div>
923
 
</div>
924
 
<div class="admonition seealso">
925
 
<p class="first admonition-title">See also</p>
926
 
<p>The <a class="reference internal" href="asyncio-protocol.html#asyncio-register-socket"><span>register an open socket to wait for data using a protocol</span></a> example uses a low-level protocol created by the
927
 
<a class="reference internal" href="#asyncio.BaseEventLoop.create_connection" title="asyncio.BaseEventLoop.create_connection"><code class="xref py py-meth docutils literal"><span class="pre">BaseEventLoop.create_connection()</span></code></a> method.</p>
928
 
<p class="last">The <a class="reference internal" href="asyncio-stream.html#asyncio-register-socket-streams"><span>register an open socket to wait for data using streams</span></a> example uses high-level streams
929
 
created by the <a class="reference internal" href="asyncio-stream.html#asyncio.open_connection" title="asyncio.open_connection"><code class="xref py py-func docutils literal"><span class="pre">open_connection()</span></code></a> function in a coroutine.</p>
930
 
</div>
931
 
</div>
932
 
<div class="section" id="set-signal-handlers-for-sigint-and-sigterm">
933
 
<h3>18.5.1.17.4. Set signal handlers for SIGINT and SIGTERM<a class="headerlink" href="#set-signal-handlers-for-sigint-and-sigterm" title="Permalink to this headline">¶</a></h3>
934
 
<p>Register handlers for signals <code class="xref py py-data docutils literal"><span class="pre">SIGINT</span></code> and <code class="xref py py-data docutils literal"><span class="pre">SIGTERM</span></code> using
935
 
the <a class="reference internal" href="#asyncio.BaseEventLoop.add_signal_handler" title="asyncio.BaseEventLoop.add_signal_handler"><code class="xref py py-meth docutils literal"><span class="pre">BaseEventLoop.add_signal_handler()</span></code></a> method:</p>
936
 
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span>
937
 
<span class="kn">import</span> <span class="nn">functools</span>
938
 
<span class="kn">import</span> <span class="nn">os</span>
939
 
<span class="kn">import</span> <span class="nn">signal</span>
940
 
 
941
 
<span class="k">def</span> <span class="nf">ask_exit</span><span class="p">(</span><span class="n">signame</span><span class="p">):</span>
942
 
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;got signal </span><span class="si">%s</span><span class="s2">: exit&quot;</span> <span class="o">%</span> <span class="n">signame</span><span class="p">)</span>
943
 
    <span class="n">loop</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
944
 
 
945
 
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
946
 
<span class="k">for</span> <span class="n">signame</span> <span class="ow">in</span> <span class="p">(</span><span class="s1">&#39;SIGINT&#39;</span><span class="p">,</span> <span class="s1">&#39;SIGTERM&#39;</span><span class="p">):</span>
947
 
    <span class="n">loop</span><span class="o">.</span><span class="n">add_signal_handler</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">signal</span><span class="p">,</span> <span class="n">signame</span><span class="p">),</span>
948
 
                            <span class="n">functools</span><span class="o">.</span><span class="n">partial</span><span class="p">(</span><span class="n">ask_exit</span><span class="p">,</span> <span class="n">signame</span><span class="p">))</span>
949
 
 
950
 
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Event loop running forever, press Ctrl+C to interrupt.&quot;</span><span class="p">)</span>
951
 
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;pid </span><span class="si">%s</span><span class="s2">: send SIGINT or SIGTERM to exit.&quot;</span> <span class="o">%</span> <span class="n">os</span><span class="o">.</span><span class="n">getpid</span><span class="p">())</span>
952
 
<span class="k">try</span><span class="p">:</span>
953
 
    <span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
954
 
<span class="k">finally</span><span class="p">:</span>
955
 
    <span class="n">loop</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
956
 
</pre></div>
957
 
</div>
958
 
<p>This example only works on UNIX.</p>
959
 
</div>
960
 
</div>
961
 
</div>
962
 
 
963
 
 
964
 
          </div>
965
 
        </div>
966
 
      </div>
967
 
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
968
 
        <div class="sphinxsidebarwrapper">
969
 
  <h3><a href="../contents.html">Table Of Contents</a></h3>
970
 
  <ul>
971
 
<li><a class="reference internal" href="#">18.5.1. Base Event Loop</a><ul>
972
 
<li><a class="reference internal" href="#run-an-event-loop">18.5.1.1. Run an event loop</a></li>
973
 
<li><a class="reference internal" href="#calls">18.5.1.2. Calls</a></li>
974
 
<li><a class="reference internal" href="#delayed-calls">18.5.1.3. Delayed calls</a></li>
975
 
<li><a class="reference internal" href="#tasks">18.5.1.4. Tasks</a></li>
976
 
<li><a class="reference internal" href="#creating-connections">18.5.1.5. Creating connections</a></li>
977
 
<li><a class="reference internal" href="#creating-listening-connections">18.5.1.6. Creating listening connections</a></li>
978
 
<li><a class="reference internal" href="#watch-file-descriptors">18.5.1.7. Watch file descriptors</a></li>
979
 
<li><a class="reference internal" href="#low-level-socket-operations">18.5.1.8. Low-level socket operations</a></li>
980
 
<li><a class="reference internal" href="#resolve-host-name">18.5.1.9. Resolve host name</a></li>
981
 
<li><a class="reference internal" href="#connect-pipes">18.5.1.10. Connect pipes</a></li>
982
 
<li><a class="reference internal" href="#unix-signals">18.5.1.11. UNIX signals</a></li>
983
 
<li><a class="reference internal" href="#executor">18.5.1.12. Executor</a></li>
984
 
<li><a class="reference internal" href="#error-handling-api">18.5.1.13. Error Handling API</a></li>
985
 
<li><a class="reference internal" href="#debug-mode">18.5.1.14. Debug mode</a></li>
986
 
<li><a class="reference internal" href="#server">18.5.1.15. Server</a></li>
987
 
<li><a class="reference internal" href="#handle">18.5.1.16. Handle</a></li>
988
 
<li><a class="reference internal" href="#event-loop-examples">18.5.1.17. Event loop examples</a><ul>
989
 
<li><a class="reference internal" href="#hello-world-with-call-soon">18.5.1.17.1. Hello World with call_soon()</a></li>
990
 
<li><a class="reference internal" href="#display-the-current-date-with-call-later">18.5.1.17.2. Display the current date with call_later()</a></li>
991
 
<li><a class="reference internal" href="#watch-a-file-descriptor-for-read-events">18.5.1.17.3. Watch a file descriptor for read events</a></li>
992
 
<li><a class="reference internal" href="#set-signal-handlers-for-sigint-and-sigterm">18.5.1.17.4. Set signal handlers for SIGINT and SIGTERM</a></li>
993
 
</ul>
994
 
</li>
995
 
</ul>
996
 
</li>
997
 
</ul>
998
 
 
999
 
  <h4>Previous topic</h4>
1000
 
  <p class="topless"><a href="asyncio.html"
1001
 
                        title="previous chapter">18.5. <code class="docutils literal"><span class="pre">asyncio</span></code> &#8211; Asynchronous I/O, event loop, coroutines and tasks</a></p>
1002
 
  <h4>Next topic</h4>
1003
 
  <p class="topless"><a href="asyncio-eventloops.html"
1004
 
                        title="next chapter">18.5.2. Event loops</a></p>
1005
 
<h3>This Page</h3>
1006
 
<ul class="this-page-menu">
1007
 
  <li><a href="../bugs.html">Report a Bug</a></li>
1008
 
  <li><a href="../_sources/library/asyncio-eventloop.txt"
1009
 
         rel="nofollow">Show Source</a></li>
1010
 
</ul>
1011
 
 
1012
 
<div id="searchbox" style="display: none" role="search">
1013
 
  <h3>Quick search</h3>
1014
 
    <form class="search" action="../search.html" method="get">
1015
 
      <input type="text" name="q" />
1016
 
      <input type="submit" value="Go" />
1017
 
      <input type="hidden" name="check_keywords" value="yes" />
1018
 
      <input type="hidden" name="area" value="default" />
1019
 
    </form>
1020
 
    <p class="searchtip" style="font-size: 90%">
1021
 
    Enter search terms or a module, class or function name.
1022
 
    </p>
1023
 
</div>
1024
 
<script type="text/javascript">$('#searchbox').show(0);</script>
1025
 
        </div>
1026
 
      </div>
1027
 
      <div class="clearer"></div>
1028
 
    </div>  
1029
 
    <div class="related" role="navigation" aria-label="related navigation">
1030
 
      <h3>Navigation</h3>
1031
 
      <ul>
1032
 
        <li class="right" style="margin-right: 10px">
1033
 
          <a href="../genindex.html" title="General Index"
1034
 
             >index</a></li>
1035
 
        <li class="right" >
1036
 
          <a href="../py-modindex.html" title="Python Module Index"
1037
 
             >modules</a> |</li>
1038
 
        <li class="right" >
1039
 
          <a href="asyncio-eventloops.html" title="18.5.2. Event loops"
1040
 
             >next</a> |</li>
1041
 
        <li class="right" >
1042
 
          <a href="asyncio.html" title="18.5. asyncio – Asynchronous I/O, event loop, coroutines and tasks"
1043
 
             >previous</a> |</li>
1044
 
        <li><img src="../_static/py.png" alt=""
1045
 
                 style="vertical-align: middle; margin-top: -1px"/></li>
1046
 
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
1047
 
        <li>
1048
 
          <span class="version_switcher_placeholder">3.5.1</span>
1049
 
          <a href="../index.html">Documentation </a> &raquo;
1050
 
        </li>
1051
 
 
1052
 
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
1053
 
          <li class="nav-item nav-item-2"><a href="ipc.html" >18. Interprocess Communication and Networking</a> &raquo;</li>
1054
 
          <li class="nav-item nav-item-3"><a href="asyncio.html" >18.5. <code class="docutils literal"><span class="pre">asyncio</span></code> &#8211; Asynchronous I/O, event loop, coroutines and tasks</a> &raquo;</li> 
1055
 
      </ul>
1056
 
    </div>  
1057
 
    <div class="footer">
1058
 
    &copy; <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
1059
 
    <br />
1060
 
    The Python Software Foundation is a non-profit corporation.
1061
 
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
1062
 
    <br />
1063
 
    Last updated on Feb 10, 2016.
1064
 
    <a href="../bugs.html">Found a bug</a>?
1065
 
    <br />
1066
 
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
1067
 
    </div>
1068
 
 
1069
 
  </body>
1070
 
</html>
 
 
b'\\ No newline at end of file'