2021-07-14 22:53:15 +08:00
|
|
|
|
import functools
|
|
|
|
|
import inspect
|
|
|
|
|
import threading
|
2021-09-02 22:47:10 +08:00
|
|
|
|
import time
|
2021-07-14 22:53:15 +08:00
|
|
|
|
from typing import Optional, Callable
|
2021-09-22 06:21:48 +08:00
|
|
|
|
from Difficult_Rocket import crash
|
2021-07-14 22:53:15 +08:00
|
|
|
|
|
|
|
|
|
"""
|
2021-09-24 14:50:42 +08:00
|
|
|
|
This part of code come from MCDReforged(https://github.com/Fallen-Breath/MCDReforged)
|
2021-07-14 22:53:15 +08:00
|
|
|
|
Very thanks to Fallen_Breath and other coder who helped MCDR worked better
|
2021-09-24 14:50:42 +08:00
|
|
|
|
GNU Lesser General Public License v3.0(GNU LGPL v3)
|
2021-09-24 14:53:12 +08:00
|
|
|
|
(have some changes)
|
2021-07-14 22:53:15 +08:00
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
2021-09-24 14:50:42 +08:00
|
|
|
|
def new_thread(thread_name: Optional[str or Callable] = None, Daemon=False):
|
2021-07-14 22:53:15 +08:00
|
|
|
|
"""
|
|
|
|
|
Use a new thread to execute the decorated function
|
|
|
|
|
The function return value will be set to the thread instance that executes this function
|
|
|
|
|
The name of the thread can be specified in parameter
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def wrapper(func):
|
|
|
|
|
@functools.wraps(func) # to preserve the origin function information
|
|
|
|
|
def wrap(*args, **kwargs):
|
2021-09-22 06:21:48 +08:00
|
|
|
|
thread_ = threading.Thread(target=func, args=args, kwargs=kwargs, name=thread_name)
|
2021-09-24 14:50:42 +08:00
|
|
|
|
thread_.setDaemon(Daemon)
|
|
|
|
|
thread_.start()
|
2021-09-24 14:52:41 +08:00
|
|
|
|
crash.all_thread.append(thread_)
|
2021-09-27 23:22:07 +08:00
|
|
|
|
return thread_
|
2021-07-14 22:53:15 +08:00
|
|
|
|
|
|
|
|
|
# bring the signature of the func to the wrap function
|
|
|
|
|
# so inspect.getfullargspec(func) works correctly
|
2021-08-13 12:25:29 +08:00
|
|
|
|
# https://stackoverflow.com/questions/39926567/python-create-decorator-preserving-function-arguments
|
2021-07-14 22:53:15 +08:00
|
|
|
|
wrap.__signature__ = inspect.signature(func)
|
|
|
|
|
return wrap
|
|
|
|
|
|
|
|
|
|
# Directly use @on_new_thread without ending brackets case
|
|
|
|
|
if isinstance(thread_name, Callable):
|
|
|
|
|
this_is_a_function = thread_name
|
|
|
|
|
thread_name = None
|
|
|
|
|
return wrapper(this_is_a_function)
|
|
|
|
|
# Use @on_new_thread with ending brackets case
|
|
|
|
|
return wrapper
|