def __init__(self, name="a"): # 私有属性: 伪私有 self.__name = name # 公有属性 self.name = name CorePrint.print_info_with_method(MyClass.__name__) pass
def run(self): CorePrint.print_info(self.name, str(self.event.is_set())) # Wait exe event.set() before run. # When timeout, continue run. # This is to say, wait and only wait t time before run. self.event.wait(self.sleep_time) CorePrint.print_info(self.name, str(self.event.is_set())) pass
def run(self): if self.is_send_pipe: for i in range(self.size): self.pipe.send(i) CorePrint.print_info("send", i, "ok") pass else: while True: r = self.pipe.recv() CorePrint.print_info("recv", r, "ok") pass
def run(self): try: for i in range(self.size): # 如果blocked为False,但该Queue已满,会立即抛出Queue.Full异常。 # 如果blocked为True(默认值),并且timeout为正值, # 该方法会阻塞timeout指定的时间,直到该队列有剩余的空间。 # 如果超时,会抛出Queue.Full异常。 self.queue.put(i, block=True, timeout=1) CorePrint.print_info("put", i, "ok") except queues.Full: CorePrint.print_info("except queues.Full") pass pass
def run(self): try: time.sleep(0.001) while True: # 如果blocked为False,有两种情况存在: # 如果Queue有一个值可用,则立即返回该值, # 否则,如果队列为空,则立即抛出Queue.Empty异常。 # 如果blocked为True(默认值),并且timeout为正值, # 那么在等待时间内没有取到任何元素,会抛出Queue.Empty异常。 value = self.queue.get(block=True, timeout=1) CorePrint.print_info("get", value, "ok") except queues.Empty: CorePrint.print_info("except queues.Empty") pass
def run_my_process_with_pool(): pool = multiprocessing.Pool(processes=3) # 关注结果 results = [] for i in range(5): # 非阻塞 # pool.apply_async(func=fun, args=(i, i)) # 阻塞 # pool.apply(func=fun, args=(i, i)) # 关注结果 results.append(pool.apply_async(func=fun, args=(i, i))) pass CorePrint.print_info("pool close") # 关闭pool,使其不在接受新的任务。 pool.close() # 主进程阻塞,等待子进程的退出,join方法要在close或terminate之后使用。 pool.join() # 关注结果 CorePrint.print_info("pool over") for result in results: CorePrint.print_info(result.get()) pass
def fun(name, sleep_time): CorePrint.print_info(name, "pool in ", os.getpid()) time.sleep(sleep_time) CorePrint.print_info(name, "pool out ", os.getpid()) return str(name) + " over" pass
def run_body(name, sleep_time): CorePrint.print_info("Process", str(name), "is sleep") time.sleep(sleep_time) CorePrint.print_info("Process", str(name), "is over")
from enum import Enum from core.CorePrint import CorePrint class Color(Enum): red = 1 orange = 2 yellow = 3 green = 4 blue = 5 indigo = 6 purple = 7 CorePrint.print_info(Color(1)) CorePrint.print_info(Color["red"]) subtype = Enum("subtype", ("plain", "html")) CorePrint.print_info(subtype.plain.name)
def get_prop(cls): # 类名 CorePrint.print_info_with_method(cls.__name__) # 父类构成的元祖 CorePrint.print_info_with_method(cls.__bases__) # CorePrint.print_info_with_method(cls.__class__) # 属性 CorePrint.print_info_with_method(cls.__dict__) # 文档字符串 CorePrint.print_info_with_method(cls.__doc__) # 模块 CorePrint.print_info_with_method(cls.__module__) # CorePrint.print_info_with_method(cls.__mro__) # CorePrint.print_info_with_method(cls.__qualname__) pass
def class_foo(cls, x): CorePrint.print_info_with_method(cls, x) pass
def static_foo(x): CorePrint.print_info_with_method(x) pass
def __foo(self, x): CorePrint.print_info_with_method(self, x) pass