郑文峰的博客 郑文峰的博客
首页
分类
标签
归档
关于
  • 导航 (opens new window)
  • 代码片段 (opens new window)
  • 收藏
  • 友链
  • 外部页面

    • 开往 (opens new window)
GitHub (opens new window)

zhengwenfeng

穷则变,变则通,通则久
首页
分类
标签
归档
关于
  • 导航 (opens new window)
  • 代码片段 (opens new window)
  • 收藏
  • 友链
  • 外部页面

    • 开往 (opens new window)
GitHub (opens new window)
  • 编程
  • python
  • tornado
zhengwenfeng
2022-08-10
目录

tornado finish和write区别

# 简介

finish和write都可以将后端的数据传输到前端。他们有啥差别嘞。

该项目的github地址: tornado_learning.git (opens new window)

# 例子

代码apps/hello/write_finish_handler.py

from tornado_learning.handler import BaseHandler
import time

class Write_Finish_Handler(BaseHandler):

    def get(self):
        self.write("hello")
        time.sleep(4)
        self.finish("world")
1
2
3
4
5
6
7
8
9

在等待4秒后,同时输出: hello world

class Finish_Write_Handler(BaseHandler):

    def get(self):
        self.finish("hello")
        self.write("world")
1
2
3
4
5

输出: hello
并且报错: Cannot write() after finish()

# 总结

self.finish()代表回应到前端的终结。并且可以在finsh后做一些与回应给前端无关的操作,缩短响应时间。
self.write()并不会马上将数据返回前端,必须在self.finsh()或者return后才会响应,类似以缓存吧。

#python#tornado
上次更新: 2023/05/01, 18:02:43
最近更新
01
使用etcd分布式锁导致的协程泄露与死锁问题
05-13
02
基于pre-commit的Python代码规范落地实践
05-12
03
初识 MCP Server
05-01
更多文章>
Theme by Vdoing | Copyright © 2022-2025 zhengwenfeng | MIT License | 赣ICP备2025055428号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式