@@ -82,8 +82,34 @@ handler functions can be defined as ``async def`` or ``def`` functions, but
82
82
``async def `` functions are recommended for performance.
83
83
84
84
The :func: `run() <microdot.Microdot.run> ` method starts the application's web
85
- server on port 5000 by default. This method blocks while it waits for
86
- connections from clients.
85
+ server on port 5000 by default, and creates its own asynchronous loop. This
86
+ method blocks while it waits for connections from clients.
87
+
88
+ For some applications it may be necessary to run the web server alongside other
89
+ asynchronous tasks, on an already running loop. In that case, instead of
90
+ ``app.run() `` the web server can be started by invoking the
91
+ :func: `start_server() <microdot.Microdot.start_server> ` coroutine as shown in
92
+ the following example::
93
+
94
+ import asyncio
95
+ from microdot import Microdot
96
+
97
+ app = Microdot()
98
+
99
+ @app.route('/')
100
+ async def index(request):
101
+ return 'Hello, world!'
102
+
103
+ async def main():
104
+ # start the server in a background task
105
+ server = asyncio.create_task(app.start_server())
106
+
107
+ # ... do other asynchronous work here ...
108
+
109
+ # cleanup before ending the application
110
+ await server
111
+
112
+ asyncio.run(main())
87
113
88
114
Running with CPython
89
115
^^^^^^^^^^^^^^^^^^^^
@@ -145,8 +171,9 @@ changed by passing the ``port`` argument to the ``run()`` method.
145
171
Web Server Configuration
146
172
^^^^^^^^^^^^^^^^^^^^^^^^
147
173
148
- The :func: `run() <microdot.Microdot.run> ` method supports a few arguments to
149
- configure the web server.
174
+ The :func: `run() <microdot.Microdot.run> ` and
175
+ :func: `start_server() <microdot.Microdot.start_server> ` methods support a few
176
+ arguments to configure the web server.
150
177
151
178
- ``port ``: The port number to listen on. Pass the desired port number in this
152
179
argument to use a port different than the default of 5000. For example::
0 commit comments