From: Kamalesh Babulal Date: Tue, 25 Jan 2022 15:47:27 +0000 (+0000) Subject: process.py: add support to spawn a multithreaded process X-Git-Tag: v3.1.0~308^2~2^2~108 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e7ce9c96ecd0fa618809b0a80a5b9e4733df3e50;p=thirdparty%2Flibcgroup.git process.py: add support to spawn a multithreaded process Add support for creating a multithreaded process. The multithreaded process gets created by spawning a new process, that in turn creates N threads. The thread function is a while loop, that sleeps for N seconds. Currently, there is no support for waiting for the threads to join, and to stop the threads, the spawned process needs to be terminated/killed. Suggested-by: Tom Hromatka Signed-off-by: Kamalesh Babulal Signed-off-by: Tom Hromatka --- diff --git a/ftests/process.py b/ftests/process.py index 53a2d739..41727f5e 100644 --- a/ftests/process.py +++ b/ftests/process.py @@ -22,6 +22,7 @@ from cgroup import Cgroup from cgroup import CgroupVersion import multiprocessing as mp +import threading as tp from run import Run from run import RunError import time @@ -38,6 +39,11 @@ class Process(object): return out_str + @staticmethod + def __thread_infinite_loop(config, sleep_time=1): + while 1: + time.sleep(sleep_time) + @staticmethod def __infinite_loop(config, sleep_time=1): cmd = ['/usr/bin/perl', '-e', '\'while(1){{sleep({})}};\''.format(sleep_time)] @@ -123,6 +129,33 @@ class Process(object): self.children.append(p) + def create_threaded_process(self, config, threads_cnt): + threads = list() + + for n in range(threads_cnt): + sleep_time = n + 1 + thread = tp.Thread(target=Process.__thread_infinite_loop, + args=(config, sleep_time, )) + threads.append(thread) + + for thread in threads: + thread.start() + + def create_threaded_process_in_cgroup(self, config, controller, cgname, + threads=2, cgclassify=True): + + p = mp.Process(target=self.create_threaded_process, + args=(config, threads, )) + p.start() + + if cgclassify: + Cgroup.classify(config, controller, cgname, p.pid) + + self.children.append(p) + self.children_pids.append(p.pid) + + return p.pid + # The caller will block until all children are stopped. def join_children(self, config): for child in self.children: