yuuki/libs/thread_control.py

40 lines
1.2 KiB
Python
Raw Normal View History

2019-12-31 21:45:14 +08:00
# -*- coding: utf-8 -*-
"""
Yuuki_Libs
(c) 2020 Star Inc.
2019-12-31 21:45:14 +08:00
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""
2019-12-25 21:35:43 +08:00
import multiprocessing
import threading
2019-08-23 18:12:41 +08:00
class Yuuki_Thread:
2019-08-22 18:33:10 +08:00
def __init__(self):
2019-08-31 12:36:18 +08:00
self.lock = threading.Lock()
@staticmethod
2020-02-28 20:53:08 +08:00
def add(function, args=()):
added_thread = threading.Thread(name=function.__name__, target=function, args=args)
2019-08-31 12:36:18 +08:00
added_thread.start()
@staticmethod
def getThreadInfo():
2019-08-31 12:36:18 +08:00
print(threading.active_count())
print(threading.enumerate())
print("{} add Threading\n".format(threading.current_thread()))
2019-12-25 21:35:43 +08:00
2019-10-11 21:43:05 +08:00
class Yuuki_Multiprocess:
2020-02-28 20:53:08 +08:00
multiprocess_list = {}
def add(self, function, args=()):
added_multiprocess = multiprocessing.Process(name=function.__name__, target=function, args=args)
self.multiprocess_list[function.__name__] = added_multiprocess
2019-08-31 12:36:18 +08:00
added_multiprocess.start()
2020-02-28 20:53:08 +08:00
def stop(self, function_name):
assert function_name in self.multiprocess_list
self.multiprocess_list[function_name].terminate()