Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gevent
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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gevent
Commits
9b2a9d84
Commit
9b2a9d84
authored
May 02, 2012
by
Denis Bilenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix examples/processes.py to use gevent.subprocess
parent
4755a234
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
68 deletions
+17
-68
examples/processes.py
examples/processes.py
+17
-68
No files found.
examples/processes.py
View file @
9b2a9d84
#!/usr/bin/env python
"""An example on how to communicate with a subprocess.
Written by Marcus Cavanaugh.
See http://groups.google.com/group/gevent/browse_thread/thread/7fca7230db0509f6
where it was first posted.
"""
import
gevent
from
gevent
import
socket
import
subprocess
import
errno
import
sys
import
os
import
fcntl
def
popen_communicate
(
args
,
data
=
''
):
"""Communicate with the process non-blockingly."""
p
=
subprocess
.
Popen
(
args
,
stdin
=
subprocess
.
PIPE
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
STDOUT
)
fcntl
.
fcntl
(
p
.
stdin
,
fcntl
.
F_SETFL
,
os
.
O_NONBLOCK
)
# make the file nonblocking
fcntl
.
fcntl
(
p
.
stdout
,
fcntl
.
F_SETFL
,
os
.
O_NONBLOCK
)
# make the file nonblocking
bytes_total
=
len
(
data
)
bytes_written
=
0
while
bytes_written
<
bytes_total
:
try
:
# p.stdin.write() doesn't return anything, so use os.write.
bytes_written
+=
os
.
write
(
p
.
stdin
.
fileno
(),
data
[
bytes_written
:])
except
IOError
:
ex
=
sys
.
exc_info
()[
1
]
if
ex
.
args
[
0
]
!=
errno
.
EAGAIN
:
raise
sys
.
exc_clear
()
socket
.
wait_write
(
p
.
stdin
.
fileno
())
p
.
stdin
.
close
()
chunks
=
[]
while
True
:
try
:
chunk
=
p
.
stdout
.
read
(
4096
)
if
not
chunk
:
break
chunks
.
append
(
chunk
)
except
IOError
:
ex
=
sys
.
exc_info
()[
1
]
if
ex
[
0
]
!=
errno
.
EAGAIN
:
raise
sys
.
exc_clear
()
socket
.
wait_read
(
p
.
stdout
.
fileno
())
from
gevent
import
subprocess
p
.
stdout
.
close
()
return
''
.
join
(
chunks
)
# run 2 jobs in parallel
p1
=
subprocess
.
Popen
([
'uname'
],
stdout
=
subprocess
.
PIPE
)
p2
=
subprocess
.
Popen
([
'finger'
],
stdout
=
subprocess
.
PIPE
)
if
__name__
==
'__main__'
:
# run 2 jobs in parallel
job1
=
gevent
.
spawn
(
popen_communicate
,
'uname'
)
job2
=
gevent
.
spawn
(
popen_communicate
,
'netstat'
)
job1
=
gevent
.
spawn
(
p1
.
stdout
.
read
)
job2
=
gevent
.
spawn
(
p2
.
stdout
.
read
)
# wait for them to complete. stop waiting after 2 seconds
gevent
.
joinall
([
job1
,
job2
],
timeout
=
5
)
# wait for them to complete. stop waiting after 2 seconds
gevent
.
joinall
([
job1
,
job2
],
timeout
=
5
)
# print the results (if available)
if
job1
.
ready
():
print
(
'finger: %s bytes: %s'
%
(
len
(
job1
.
value
or
''
),
repr
(
job1
.
value
)[:
50
]))
else
:
print
(
'finger: job is still running'
)
if
job2
.
ready
():
print
(
'netstat: %s bytes: %s'
%
(
len
(
job2
.
value
or
''
),
repr
(
job2
.
value
)[:
50
]))
else
:
print
(
'netstat: job is still running'
)
# print the results (if available)
if
job1
.
ready
():
print
(
'finger: %s bytes: %s'
%
(
len
(
job1
.
value
or
''
),
repr
(
job1
.
value
)[:
50
]))
else
:
print
(
'finger: job is still running'
)
if
job2
.
ready
():
print
(
'netstat: %s bytes: %s'
%
(
len
(
job2
.
value
or
''
),
repr
(
job2
.
value
)[:
50
]))
else
:
print
(
'netstat: job is still running'
)
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