Change in osmo-gsm-tester[master]: process: Introduce and use an asyncio strategy for killing

Holger Freyther gerrit-no-reply at lists.osmocom.org
Fri Mar 1 08:15:13 UTC 2019


Holger Freyther has uploaded this change for review. ( https://gerrit.osmocom.org/13088


Change subject: process: Introduce and use an asyncio strategy for killing
......................................................................

process: Introduce and use an asyncio strategy for killing

Use asyncio to concurrently terminate all processes.

Change-Id: I3441e280241a5ee1404623f1f0b5faf56271227d
---
M src/osmo_gsm_tester/process.py
M src/osmo_gsm_tester/suite.py
2 files changed, 51 insertions(+), 2 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-gsm-tester refs/changes/88/13088/1

diff --git a/src/osmo_gsm_tester/process.py b/src/osmo_gsm_tester/process.py
index bb13131..c3ca4ba 100644
--- a/src/osmo_gsm_tester/process.py
+++ b/src/osmo_gsm_tester/process.py
@@ -17,6 +17,7 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+import asyncio
 import os
 import time
 import subprocess
@@ -118,6 +119,22 @@
             self._poll_for_termination()
 
 
+class AsyncioTerminationStrategy(TerminationStrategy):
+    """Using asyncio to terminate all processes."""
+
+    # The asyncio event loop to use.
+    loop = asyncio.get_event_loop()
+
+    def terminate_all(self):
+        # TODO(zecke): Make this use asyncio.run when moving to python 3.7.
+        self.loop.run_until_complete(self.do_terminate_all())
+        self.log("Deletion completed")
+
+    async def do_terminate_all(self):
+        calls = [proc.terminate_async() for proc in self._processes]
+        await asyncio.gather(*calls)
+
+
 class Process(log.Origin):
 
     def __init__(self, name, run_dir, popen_args, **popen_kwargs):
@@ -207,6 +224,22 @@
             time.sleep(wait_step)
         return False
 
+    async def _poll_termination_async(self, time_to_wait_for_term=5):
+        wait_step = 0.001
+        waited_time = 0
+        while True:
+            # poll returns None if proc is still running
+            self.result = self.process_obj.poll()
+            if self.result is not None:
+                return True
+            waited_time += wait_step
+            # make wait_step approach 1.0
+            wait_step = (1. + 5. * wait_step) / 6.
+            if waited_time >= time_to_wait_for_term:
+                break
+            asyncio.sleep(wait_step)
+        return False
+
     def send_signal(self, sig):
         os.kill(self.process_obj.pid, sig)
 
@@ -221,6 +254,22 @@
         self.send_signal(sig)
         self.killed = sig
 
+    async def terminate_async(self):
+        """Terminates this process using asyncio."""
+        if self.process_obj is None:
+            return
+        if self.result is not None:
+            return
+
+        for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGKILL]:
+            self.kill(sig)
+            if sig == signal.SIGKILL:
+                continue
+            if await self._poll_termination_async():
+                break
+        self.process_obj.wait()
+        self.cleanup()
+
     def terminate(self):
         if self.process_obj is None:
             return
@@ -240,7 +289,7 @@
 
             # out of patience
             self.kill(signal.SIGKILL)
-            break;
+            break
 
         self.process_obj.wait()
         self.cleanup()
diff --git a/src/osmo_gsm_tester/suite.py b/src/osmo_gsm_tester/suite.py
index 39da917..1a32963 100644
--- a/src/osmo_gsm_tester/suite.py
+++ b/src/osmo_gsm_tester/suite.py
@@ -246,7 +246,7 @@
         self._processes.insert(0, (process, respawn))
 
     def stop_processes(self):
-        strategy = process.ParallelTerminationStrategy()
+        strategy = process.AsyncioTerminationStrategy()
         while self._processes:
             proc, _ = self._processes.pop()
             strategy.add_process(proc)

-- 
To view, visit https://gerrit.osmocom.org/13088
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-gsm-tester
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I3441e280241a5ee1404623f1f0b5faf56271227d
Gerrit-Change-Number: 13088
Gerrit-PatchSet: 1
Gerrit-Owner: Holger Freyther <holger at freyther.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20190301/6ab6a89a/attachment.html>


More information about the gerrit-log mailing list