Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mitogen
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
mitogen
Commits
db225638
Commit
db225638
authored
Sep 21, 2017
by
David Wilson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core: Make iter_read() handle deadline (and non-blocking IO) properly
parent
092bbef0
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
79 additions
and
16 deletions
+79
-16
mitogen/master.py
mitogen/master.py
+13
-16
tests/data/iter_read_generator.sh
tests/data/iter_read_generator.sh
+10
-0
tests/master_test.py
tests/master_test.py
+56
-0
No files found.
mitogen/master.py
View file @
db225638
...
...
@@ -167,27 +167,22 @@ def write_all(fd, s):
return
written
def
read_with_deadline
(
fd
,
size
,
deadline
):
timeout
=
deadline
-
time
.
time
()
if
timeout
>
0
:
rfds
,
_
,
_
=
select
.
select
([
fd
],
[],
[],
timeout
)
if
rfds
:
return
os
.
read
(
fd
,
size
)
raise
mitogen
.
core
.
TimeoutError
(
'read timed out'
)
def
iter_read
(
fd
,
deadline
=
None
):
bits
=
[]
timeout
=
None
def
iter_read
(
fd
,
deadline
)
:
while
True
:
if
deadline
is
not
None
:
LOG
.
debug
(
'Warning: iter_read(.., deadline=...) unimplemented'
)
timeout
=
max
(
0
,
deadline
-
time
.
time
())
if
timeout
==
0
:
break
rfds
,
_
,
_
=
select
.
select
([
fd
],
[],
[],
timeout
)
if
not
rfds
:
continue
bits
=
[]
while
True
:
s
,
disconnected
=
mitogen
.
core
.
io_op
(
os
.
read
,
fd
,
4096
)
if
disconnected
:
s
=
''
if
not
s
:
raise
mitogen
.
core
.
StreamError
(
'EOF on stream; last 300 bytes received: %r'
%
(
''
.
join
(
bits
)[
-
300
:],)
...
...
@@ -196,6 +191,8 @@ def iter_read(fd, deadline):
bits
.
append
(
s
)
yield
s
raise
mitogen
.
core
.
TimeoutError
(
'read timed out'
)
def
discard_until
(
fd
,
s
,
deadline
):
for
buf
in
iter_read
(
fd
,
deadline
):
...
...
tests/data/iter_read_generator.sh
0 → 100755
View file @
db225638
#!/bin/bash
# I produce text every 100ms, for testing mitogen.core.iter_read()
i
=
0
while
:
;
do
i
=
$((
$i
+
1
))
echo
"
$i
"
sleep
0.1
done
tests/master_test.py
0 → 100644
View file @
db225638
import
subprocess
import
time
import
unittest
import
testlib
import
mitogen.master
class
IterReadTest
(
unittest
.
TestCase
):
func
=
staticmethod
(
mitogen
.
master
.
iter_read
)
def
make_proc
(
self
):
args
=
[
testlib
.
data_path
(
'iter_read_generator.sh'
)]
return
subprocess
.
Popen
(
args
,
stdout
=
subprocess
.
PIPE
)
def
test_no_deadline
(
self
):
proc
=
self
.
make_proc
()
try
:
reader
=
self
.
func
(
proc
.
stdout
.
fileno
())
for
i
,
chunk
in
enumerate
(
reader
,
1
):
assert
i
==
int
(
chunk
)
if
i
>
3
:
break
finally
:
proc
.
terminate
()
def
test_deadline_exceeded_before_call
(
self
):
proc
=
self
.
make_proc
()
reader
=
self
.
func
(
proc
.
stdout
.
fileno
(),
0
)
try
:
got
=
[]
try
:
for
chunk
in
reader
:
got
.
append
(
chunk
)
assert
0
,
'TimeoutError not raised'
except
mitogen
.
core
.
TimeoutError
:
assert
len
(
got
)
==
0
finally
:
proc
.
terminate
()
def
test_deadline_exceeded_during_call
(
self
):
proc
=
self
.
make_proc
()
reader
=
self
.
func
(
proc
.
stdout
.
fileno
(),
time
.
time
()
+
0.4
)
try
:
got
=
[]
try
:
for
chunk
in
reader
:
got
.
append
(
chunk
)
assert
0
,
'TimeoutError not raised'
except
mitogen
.
core
.
TimeoutError
:
# Give a little wiggle room in case of imperfect scheduling.
# Ideal number should be 9.
assert
3
<
len
(
got
)
<
5
finally
:
proc
.
terminate
()
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