-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #479 from ganisback/support-stream
feat: support stream api
- Loading branch information
Showing
4 changed files
with
168 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import asyncio | ||
|
||
import tornado.escape | ||
import tornado.ioloop | ||
import tornado.options | ||
import tornado.web | ||
import tornado.websocket | ||
from tornado.options import define, options | ||
|
||
|
||
class Application(tornado.web.Application): | ||
def __init__(self): | ||
handlers = [ | ||
(r"/stream/(\d+)", StreamHandler), | ||
] | ||
super().__init__(handlers) | ||
|
||
|
||
class StreamHandler(tornado.web.RequestHandler): | ||
async def get(self, seconds): | ||
for i in range(int(seconds)): | ||
await asyncio.sleep(0.5) | ||
self.write(f"data: {i}\n\n") | ||
await self.flush() | ||
|
||
|
||
def main(): | ||
define("port", default=8888, help="run on the given port", type=int) | ||
options.parse_command_line() | ||
app = Application() | ||
app.listen(options.port) | ||
tornado.ioloop.IOLoop.current().start() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters