Skip to content

Commit 347a887

Browse files
committed
experiment with treading. no better.
1 parent e41044c commit 347a887

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

hobbler.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
-h --help Show this screen.
1414
--testing Testing mode (faster loop)
1515
"""
16-
import asyncio
1716
from collections import namedtuple
1817
from docopt import docopt
1918
import os
2019
import psutil
2120
import signal
21+
import time
22+
import threading
2223

2324
HOBBLING = 'hobbling pid {}'
2425
HOBBLING_CHILD = 'hobbling child pid {}'
@@ -49,7 +50,6 @@ def get_top_level_processes(cgroup_dir):
4950
return parents
5051

5152

52-
@asyncio.coroutine
5353
def hobble_process_tree(parent):
5454
print(HOBBLING.format(parent.pid))
5555
while True:
@@ -58,50 +58,41 @@ def hobble_process_tree(parent):
5858
for child in parent.children:
5959
print(HOBBLING_CHILD.format(child))
6060
os.kill(child, signal.SIGSTOP)
61-
yield from asyncio.sleep(0.25)
61+
time.sleep(0.25)
6262
for child in reversed(parent.children):
6363
os.kill(child, signal.SIGCONT)
6464
os.kill(parent.pid, signal.SIGCONT)
65-
yield from asyncio.sleep(0.01)
65+
time.sleep(0.01)
6666

6767
except ProcessLookupError:
6868
print(HOBBLED_PROCESS_DIED.format(parent.pid))
6969
break
7070

7171

72-
@asyncio.coroutine
73-
def hobble_current_processes(loop, already_hobbled, cgroup_dir):
72+
def hobble_current_processes(already_hobbled, cgroup_dir):
7473
print('getting latest process list')
7574
parents = get_top_level_processes(cgroup_dir)
7675
for parent in parents:
7776
if parent.pid in already_hobbled:
7877
continue
7978
already_hobbled.add(parent.pid)
80-
loop.create_task(
81-
hobble_process_tree(parent)
82-
)
79+
threading.Thread(target=lambda: hobble_process_tree(parent)).start()
8380
print('now hobbling {} processes'.format(len(already_hobbled)))
8481

8582

86-
@asyncio.coroutine
87-
def hobble_processes_forever(loop, cgroup_dir, tarpit_update_pause):
83+
def hobble_processes_forever(cgroup_dir, tarpit_update_pause):
8884
already_hobbled = set()
8985
while True:
9086
print('hobbling...', flush=True)
91-
yield from hobble_current_processes(loop, already_hobbled, cgroup_dir)
92-
yield from asyncio.sleep(tarpit_update_pause)
87+
threading.Thread(
88+
target=lambda: hobble_current_processes(already_hobbled, cgroup_dir)
89+
).start()
90+
time.sleep(tarpit_update_pause)
9391

9492

9593
def main(cgroup_dir, tarpit_update_pause):
9694
print('Starting process hobbler')
97-
loop = asyncio.get_event_loop()
98-
if not hasattr(loop, 'create_task'): # was added in 3.4.2
99-
loop.create_task = asyncio.async
100-
loop.create_task(
101-
hobble_processes_forever(loop, cgroup_dir, tarpit_update_pause)
102-
)
103-
loop.run_forever()
104-
loop.close()
95+
hobble_processes_forever(cgroup_dir, tarpit_update_pause)
10596

10697

10798
if __name__ == '__main__':

0 commit comments

Comments
 (0)