yuuki/libs/thread.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
2021-03-10 21:23:53 +08:00
class 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
2020-12-19 03:31:48 +08:00
def get_thread_info():
2019-08-31 12:36:18 +08:00
print(threading.active_count())
print(threading.enumerate())
2020-12-19 03:31:48 +08:00
print(f"{threading.current_thread()} add Threading\n")
2019-08-31 12:36:18 +08:00
2019-12-25 21:35:43 +08:00
2021-03-10 21:23:53 +08:00
class 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()