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

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

zhengwenfeng

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

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

    • 基础

    • 第三方库

    • django

    • flask

    • tornado

      • tornado 文件上传
      • tornado 使用jwt完成用户异步认证
      • tornado 用户密码 bcrypt加密
      • tornado 结合wtforms使用表单操作
      • tornado finish和write区别
        • 简介
        • 例子
        • 总结
      • tornado 使用peewee-async 完成异步orm数据库操作
    • 其他

  • go语言

  • linux

  • 其他

  • 编程
  • 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
上次更新: 2025/05/07, 05:17:56
tornado 结合wtforms使用表单操作
tornado 使用peewee-async 完成异步orm数据库操作

← tornado 结合wtforms使用表单操作 tornado 使用peewee-async 完成异步orm数据库操作→

最近更新
01
Go语言高效IO缓冲技术详解
06-14
02
Go语言延迟初始化(Lazy Initialization)最佳实践
06-14
03
Go语言字符串拼接性能对比与优化指南
06-14
更多文章>
Theme by Vdoing | Copyright © 2022-2025 zhengwenfeng | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式