Example processes.py#
1#!/usr/bin/env python
2from __future__ import print_function
3import gevent
4from gevent import subprocess
5
6import sys
7
8if sys.platform.startswith('win'):
9 UNAME = ['cmd.exe', '/C', 'ver']
10 LS = ['dir.exe']
11else:
12 UNAME = ['uname']
13 LS = ['ls']
14
15# run 2 jobs in parallel
16p1 = subprocess.Popen(UNAME, stdout=subprocess.PIPE)
17p2 = subprocess.Popen(LS, stdout=subprocess.PIPE)
18
19gevent.wait([p1, p2], timeout=2)
20
21# print the results (if available)
22if p1.poll() is not None:
23 print('uname: %r' % p1.stdout.read())
24else:
25 print('uname: job is still running')
26if p2.poll() is not None:
27 print('ls: %r' % p2.stdout.read())
28else:
29 print('ls: job is still running')
30
31p1.stdout.close()
32p2.stdout.close()