Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
镜像
tornadoweb
Tornado
提交
6c7d44bd
Tornado
项目概览
镜像
/
tornadoweb
/
Tornado
10 个月 前同步成功
通知
26
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Tornado
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
6c7d44bd
编写于
6月 19, 2023
作者:
B
Ben Darnell
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
asyncio: Modernize type annotations
上级
c3b8ee8d
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
38 addition
and
30 deletion
+38
-30
tornado/platform/asyncio.py
tornado/platform/asyncio.py
+38
-30
未找到文件。
tornado/platform/asyncio.py
浏览文件 @
6c7d44bd
...
...
@@ -36,23 +36,33 @@ import warnings
from
tornado.gen
import
convert_yielded
from
tornado.ioloop
import
IOLoop
,
_Selectable
from
typing
import
Any
,
TypeVar
,
Awaitable
,
Callable
,
Union
,
Optional
,
List
,
Dict
if
typing
.
TYPE_CHECKING
:
from
typing
import
Set
,
Tuple
# noqa: F401
from
typing_extensions
import
Protocol
from
typing
import
(
Any
,
Awaitable
,
Callable
,
Dict
,
List
,
Optional
,
Protocol
,
Set
,
Tuple
,
TypeVar
,
Union
,
)
class
_HasFileno
(
Protocol
):
def
fileno
(
self
)
->
int
:
pass
class
_HasFileno
(
Protocol
):
def
fileno
(
self
)
->
int
:
pass
_FileDescriptorLike
=
Union
[
int
,
_HasFileno
]
_FileDescriptorLike
=
Union
[
int
,
_HasFileno
]
_T
=
TypeVar
(
"_T"
)
# Collection of selector thread event loops to shut down on exit.
_selector_loops
=
set
()
# type: Set[SelectorThread]
_selector_loops
:
Set
[
"SelectorThread"
]
=
set
()
def
_atexit_callback
()
->
None
:
...
...
@@ -88,16 +98,16 @@ class BaseAsyncIOLoop(IOLoop):
# as windows where the default event loop does not implement these methods.
self
.
selector_loop
=
asyncio_loop
if
hasattr
(
asyncio
,
"ProactorEventLoop"
)
and
isinstance
(
asyncio_loop
,
asyncio
.
ProactorEventLoop
# type: ignore
asyncio_loop
,
asyncio
.
ProactorEventLoop
):
# Ignore this line for mypy because the abstract method checker
# doesn't understand dynamic proxies.
self
.
selector_loop
=
AddThreadSelectorEventLoop
(
asyncio_loop
)
# type: ignore
# Maps fd to (fileobj, handler function) pair (as in IOLoop.add_handler)
self
.
handlers
=
{}
# type: Dict[int, Tuple[Union[int, _Selectable], Callable]]
self
.
handlers
:
Dict
[
int
,
Tuple
[
Union
[
int
,
_Selectable
],
Callable
]]
=
{}
# Set of fds listening for reads/writes
self
.
readers
=
set
()
# type: Set[int]
self
.
writers
=
set
()
# type: Set[int]
self
.
readers
:
Set
[
int
]
=
set
()
self
.
writers
:
Set
[
int
]
=
set
()
self
.
closing
=
False
# If an asyncio loop was closed through an asyncio interface
# instead of IOLoop.close(), we'd never hear about it and may
...
...
@@ -419,9 +429,7 @@ class AnyThreadEventLoopPolicy(_BasePolicy): # type: ignore
def
get_event_loop
(
self
)
->
asyncio
.
AbstractEventLoop
:
try
:
return
super
().
get_event_loop
()
except
(
RuntimeError
,
AssertionError
):
# This was an AssertionError in Python 3.4.2 (which ships with Debian Jessie)
# and changed to a RuntimeError in 3.4.3.
except
RuntimeError
:
# "There is no current event loop in thread %r"
loop
=
self
.
new_event_loop
()
self
.
set_event_loop
(
loop
)
...
...
@@ -445,11 +453,11 @@ class SelectorThread:
self
.
_real_loop
=
real_loop
self
.
_select_cond
=
threading
.
Condition
()
self
.
_select_args
=
(
None
)
# type: Optional[Tuple[List[_FileDescriptorLike], List[_FileDescriptorLike]]]
self
.
_select_args
:
Optional
[
Tuple
[
List
[
_FileDescriptorLike
],
List
[
_FileDescriptorLike
]]
]
=
None
self
.
_closing_selector
=
False
self
.
_thread
=
None
# type: Optional[threading.Thread]
self
.
_thread
:
Optional
[
threading
.
Thread
]
=
None
self
.
_thread_manager_handle
=
self
.
_thread_manager
()
async
def
thread_manager_anext
()
->
None
:
...
...
@@ -464,8 +472,8 @@ class SelectorThread:
lambda
:
self
.
_real_loop
.
create_task
(
thread_manager_anext
())
)
self
.
_readers
=
{}
# type: Dict[_FileDescriptorLike, Callable]
self
.
_writers
=
{}
# type: Dict[_FileDescriptorLike, Callable]
self
.
_readers
:
Dict
[
_FileDescriptorLike
,
Callable
]
=
{}
self
.
_writers
:
Dict
[
_FileDescriptorLike
,
Callable
]
=
{}
# Writing to _waker_w will wake up the selector thread, which
# watches for _waker_r to be readable.
...
...
@@ -603,7 +611,7 @@ class SelectorThread:
pass
def
_handle_select
(
self
,
rs
:
List
[
"_FileDescriptorLike"
],
ws
:
List
[
"_FileDescriptorLike"
]
self
,
rs
:
List
[
_FileDescriptorLike
],
ws
:
List
[
_FileDescriptorLike
]
)
->
None
:
for
r
in
rs
:
self
.
_handle_event
(
r
,
self
.
_readers
)
...
...
@@ -613,8 +621,8 @@ class SelectorThread:
def
_handle_event
(
self
,
fd
:
"_FileDescriptorLike"
,
cb_map
:
Dict
[
"_FileDescriptorLike"
,
Callable
],
fd
:
_FileDescriptorLike
,
cb_map
:
Dict
[
_FileDescriptorLike
,
Callable
],
)
->
None
:
try
:
callback
=
cb_map
[
fd
]
...
...
@@ -623,18 +631,18 @@ class SelectorThread:
callback
()
def
add_reader
(
self
,
fd
:
"_FileDescriptorLike"
,
callback
:
Callable
[...,
None
],
*
args
:
Any
self
,
fd
:
_FileDescriptorLike
,
callback
:
Callable
[...,
None
],
*
args
:
Any
)
->
None
:
self
.
_readers
[
fd
]
=
functools
.
partial
(
callback
,
*
args
)
self
.
_wake_selector
()
def
add_writer
(
self
,
fd
:
"_FileDescriptorLike"
,
callback
:
Callable
[...,
None
],
*
args
:
Any
self
,
fd
:
_FileDescriptorLike
,
callback
:
Callable
[...,
None
],
*
args
:
Any
)
->
None
:
self
.
_writers
[
fd
]
=
functools
.
partial
(
callback
,
*
args
)
self
.
_wake_selector
()
def
remove_reader
(
self
,
fd
:
"_FileDescriptorLike"
)
->
bool
:
def
remove_reader
(
self
,
fd
:
_FileDescriptorLike
)
->
bool
:
try
:
del
self
.
_readers
[
fd
]
except
KeyError
:
...
...
@@ -642,7 +650,7 @@ class SelectorThread:
self
.
_wake_selector
()
return
True
def
remove_writer
(
self
,
fd
:
"_FileDescriptorLike"
)
->
bool
:
def
remove_writer
(
self
,
fd
:
_FileDescriptorLike
)
->
bool
:
try
:
del
self
.
_writers
[
fd
]
except
KeyError
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录