Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
ade04126
Commit
ade04126
authored
Nov 05, 2015
by
Yury Selivanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
asyncio: Optimize asyncio.sleep(0)
parent
5be2dac5
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
0 deletions
+28
-0
Lib/asyncio/tasks.py
Lib/asyncio/tasks.py
+4
-0
Lib/test/test_asyncio/test_tasks.py
Lib/test/test_asyncio/test_tasks.py
+24
-0
No files found.
Lib/asyncio/tasks.py
View file @
ade04126
...
...
@@ -488,6 +488,10 @@ def as_completed(fs, *, loop=None, timeout=None):
@
coroutine
def
sleep
(
delay
,
result
=
None
,
*
,
loop
=
None
):
"""Coroutine that completes after a given time (in seconds)."""
if
delay
==
0
:
yield
return
result
future
=
futures
.
Future
(
loop
=
loop
)
h
=
future
.
_loop
.
call_later
(
delay
,
future
.
_set_result_unless_cancelled
,
result
)
...
...
Lib/test/test_asyncio/test_tasks.py
View file @
ade04126
...
...
@@ -2188,5 +2188,29 @@ class RunCoroutineThreadsafeTests(test_utils.TestCase):
self
.
assertEqual
(
context
[
'exception'
],
exc_context
.
exception
)
class
SleepTests
(
test_utils
.
TestCase
):
def
setUp
(
self
):
self
.
loop
=
asyncio
.
new_event_loop
()
asyncio
.
set_event_loop
(
None
)
def
test_sleep_zero
(
self
):
result
=
0
def
inc_result
(
num
):
nonlocal
result
result
+=
num
@
asyncio
.
coroutine
def
coro
():
self
.
loop
.
call_soon
(
inc_result
,
1
)
self
.
assertEqual
(
result
,
0
)
num
=
yield
from
asyncio
.
sleep
(
0
,
loop
=
self
.
loop
,
result
=
10
)
self
.
assertEqual
(
result
,
1
)
# inc'ed by call_soon
inc_result
(
num
)
# num should be 11
self
.
loop
.
run_until_complete
(
coro
())
self
.
assertEqual
(
result
,
11
)
if
__name__
==
'__main__'
:
unittest
.
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment