def __del__(self): if ( not self.asynchronous and hasattr(self, "_loop_runner") and not sys.is_finalizing() ): self.close()
def test_is_finalizing(self): self.assertIs(sys.is_finalizing(), False) # Don't use the atexit module because _Py_Finalizing is only set # after calling atexit callbacks code = """if 1: import sys class AtExit: is_finalizing = sys.is_finalizing print = print def __del__(self): self.print(self.is_finalizing(), flush=True) # Keep a reference in the __main__ module namespace, so the # AtExit destructor will be called at Python exit ref = AtExit() """ rc, stdout, stderr = assert_python_ok('-c', code) if test.support.check_impl_detail(cpython=True): self.assertEqual(stdout.rstrip(), b'True') else: # the __del__ method may or may not have been called # in other Python implementations self.assertIn(stdout.rstrip(), {b'True', b''})
def _reclaim_dead(refback): # be careful: data is dead and we're in GC! if sys.is_finalizing(): return d = refback[0] if not d: return handle = d.__handle if type(handle) is not _Handle: return #assert(d.__handle.watch_target() == None) # d is dead: if watch_target is live, it have lost a reference to d. # It means that d is safe to be returned to pool. pool = handle.datapool() DataPool._clear_handle_content(handle, finalizer_detach=False) d.__handle = IN_POOL if not pool: return if pool._gc_recovery: new_d = pool._gc_recovery(d) if new_d: pool._check_cleanness(new_d, in_pool_ok=True) pool._reclaimed.append(new_d) # We're inside GC! # pool._append_to_queue is not useful because # deque.append causes deadlock else: warnings.warn( UserWarning( "DataPool: an object collected during cyclic garbage collection; discarded" ))
def stop(self): if self.is_running() and not sys.is_finalizing(): self._file_watcher.stop() self._file_watcher.join() self._config_watch = None else: raise AutoloaderError('autoloader is not running')
def __del__(self): if not hasattr(self, "gateway"): return if self.asynchronous: # No del for async mode return if not sys.is_finalizing(): self.close()
def close(self): super().close() if not sys.is_finalizing(): for sig in list(self._signal_handlers): self.remove_signal_handler(sig) else: warinigs.warn(f"Closing the loop {self!r} on interpreter shutdown " f"stage, signal unsubsription is disabled", ResourceWarning, source=self)
def object_deleted(self, wref): # App is shutting down try: if sys.is_finalizing(): return except AttributeError: if sys is None: return sender = self.__senders.pop(self.token, None) if sender is not None: sender.disconnect(self.token)
def object_deleted(self, wref): # App is shutting down try: if sys.is_finalizing(): return except AttributeError: if sys is None: return if self.token: self.token.remove() self.token = None
def object_deleted(self, wref): # App is shutting down try: if sys.is_finalizing(): return except AttributeError: if sys is None: return sender = self.__senders.pop(self.token, None) if sender is not None: sender.disconnect(self.token)
def object_deleted(self, wref): # App is shutting down try: if sys.is_finalizing(): return except AttributeError: if sys is None: return if self.token: self.token.remove() self.token = None
def close(self): super().close() if not sys.is_finalizing(): for sig in list(self._signal_handlers): self.remove_signal_handler(sig) else: if self._signal_handlers: warnings.warn(f"Closing the loop {self!r} " f"on interpreter shutdown " f"stage, skipping signal handlers removal", ResourceWarning, source=self) self._signal_handlers.clear()
def close(self): super().close() if not sys.is_finalizing(): for sig in list(self._signal_handlers): self.remove_signal_handler(sig) else: if self._signal_handlers: warnings.warn(f"Closing the loop {self!r} " f"on interpreter shutdown " f"stage, skipping signal handlers removal", ResourceWarning, source=self) self._signal_handlers.clear()
async def read(self, deserializers=None): stream = self.stream if stream is None: raise CommClosedError() fmt = "Q" fmt_size = struct.calcsize(fmt) try: frames_nbytes = await stream.read_bytes(fmt_size) (frames_nbytes, ) = struct.unpack(fmt, frames_nbytes) frames = host_array(frames_nbytes) for i, j in sliding_window( 2, range(0, frames_nbytes + OPENSSL_MAX_CHUNKSIZE, OPENSSL_MAX_CHUNKSIZE), ): chunk = frames[i:j] chunk_nbytes = len(chunk) n = await stream.read_into(chunk) assert n == chunk_nbytes, (n, chunk_nbytes) except StreamClosedError as e: self.stream = None self._closed = True if not sys.is_finalizing(): convert_stream_closed_error(self, e) except Exception: # Some OSError or a another "low-level" exception. We do not really know what # was already read from the underlying socket, so it is not even safe to retry # here using the same stream. The only safe thing to do is to abort. # (See also GitHub #4133). self.abort() raise else: try: frames = unpack_frames(frames) msg = await from_frames( frames, deserialize=self.deserialize, deserializers=deserializers, allow_offload=self.allow_offload, ) except EOFError: # Frames possibly garbled or truncated by communication error self.abort() raise CommClosedError("aborted stream on truncated data") return msg
def __setCurrentItem(self, item): """Handle change of current item""" if sys.is_finalizing(): return previousItem = self.getCurrentItem() if item != previousItem: if previousItem is not None: previousItem.sigItemChanged.disconnect(self.__itemUpdated) self.__currentItem = item if self.__currentItem is not None: self.__currentItem.sigItemChanged.connect(self.__itemUpdated) self.sigCurrentItemChanged.emit(self.__currentItem)
def _reclaim(refback): # called either from finalizer or as weakref callback if sys.is_finalizing(): # meaningless to return objects to pools upon exit return d = refback[0] if not d: return handle = d.__handle if type(handle) is not _Handle: return assert (d.__handle.watch_target() == None) pool = handle.datapool() DataPool._clear_handle_content(handle, finalizer_detach=False) d.__handle = IN_POOL if pool: pool._append_to_queue(d)
def test_is_finalizing(self): self.assertIs(sys.is_finalizing(), False) code = """if 1: import sys class AtExit: is_finalizing = sys.is_finalizing print = print def __del__(self): self.print(self.is_finalizing(), flush=True) # Keep a reference in the __main__ module namespace, so the # AtExit destructor will be called at Python exit ref = AtExit() """ rc, stdout, stderr = assert_python_ok('-c', code) self.assertEqual(stdout.rstrip(), b'True')
def shutdown(self, status="SUCCEEDED", diagnostics=None): """Shutdown the application. Parameters ---------- status : {'SUCCEEDED', 'FAILED', 'KILLED'}, optional The yarn application exit status. diagnostics : str, optional The application exit message, usually used for diagnosing failures. Can be seen in the YARN Web UI for completed applications under "diagnostics". If not provided, a default will be used. """ if self.asynchronous: return self._stop_internal(status=status, diagnostics=diagnostics) if self.loop.asyncio_loop.is_running() and not sys.is_finalizing(): self._sync(self._stop_internal(status=status, diagnostics=diagnostics)) else: # Always run this! self._stop_sync(status=status, diagnostics=diagnostics) self._loop_runner.stop()
def test_is_finalizing(self): self.assertIs(sys.is_finalizing(), False) # Don't use the atexit module because _Py_Finalizing is only set # after calling atexit callbacks code = """if 1: import sys class AtExit: is_finalizing = sys.is_finalizing print = print def __del__(self): self.print(self.is_finalizing(), flush=True) # Keep a reference in the __main__ module namespace, so the # AtExit destructor will be called at Python exit ref = AtExit() """ rc, stdout, stderr = assert_python_ok('-c', code) self.assertEqual(stdout.rstrip(), b'True')
def close(self): if self._signal_handlers: if sys.is_finalizing(): warnings.warn( f"Closing the loop {self!r} " f"on interpreter shutdown " f"stage, skipping signal handlers removal", ResourceWarning, source=self, ) self._signal_handlers.clear() else: for signum in list(self._signal_handlers): self.remove_signal_handler(signum) self._remove_reader(self._ssock.fileno()) self._ssock.close() self._ssock = None self._csock.close() self._csock = None
def signal_handling( handler: ta.Union['signal.Handlers', ta.Callable[[int, ta.Any], None]], sigs: ta.Iterable[int] ): if not callable(handler): raise TypeError(handler) sigs = list(sigs) previous_handlers = [ signal.signal( sig, handler if not isinstance(handler, signal.Handlers) else functools.partial(handler, sig)) for sig in sigs] try: yield finally: # Per https://bugs.python.org/msg236894 don't bother when interpreter is shutting down if not sys.is_finalizing(): for sig, previous_handler in zip(sigs, previous_handlers): signal.signal(sig, previous_handler)
def error(self, msg): tosend = str(self.env.now) + ';' + msg if sys.is_finalizing() == False: self.logger.error(tosend)
def test_is_finalizing(self): import sys assert not sys.is_finalizing()
async def write(self, msg, serializers=None, on_error="message"): stream = self.stream if stream is None: raise CommClosedError() frames = await to_frames( msg, allow_offload=self.allow_offload, serializers=serializers, on_error=on_error, context={ "sender": self.local_info, "recipient": self.remote_info, **self.handshake_options, }, ) frames_nbytes = [nbytes(f) for f in frames] frames_nbytes_total = sum(frames_nbytes) header = pack_frames_prelude(frames) header = struct.pack("Q", nbytes(header) + frames_nbytes_total) + header frames = [header, *frames] frames_nbytes = [nbytes(header), *frames_nbytes] frames_nbytes_total += frames_nbytes[0] if frames_nbytes_total < 2**17: # 128kiB # small enough, send in one go frames = [b"".join(frames)] frames_nbytes = [frames_nbytes_total] try: # trick to enque all frames for writing beforehand for each_frame_nbytes, each_frame in zip(frames_nbytes, frames): if each_frame_nbytes: if stream._write_buffer is None: raise StreamClosedError() if isinstance(each_frame, memoryview): # Make sure that `len(data) == data.nbytes` # See <https://github.com/tornadoweb/tornado/pull/2996> each_frame = memoryview(each_frame).cast("B") stream._write_buffer.append(each_frame) stream._total_write_index += each_frame_nbytes # start writing frames stream.write(b"") except StreamClosedError as e: self.stream = None self._closed = True if not sys.is_finalizing(): convert_stream_closed_error(self, e) except Exception: # Some OSError or a another "low-level" exception. We do not really know # what was already written to the underlying socket, so it is not even safe # to retry here using the same stream. The only safe thing to do is to # abort. (See also GitHub #4133). self.abort() raise return frames_nbytes_total
def start(self): log_info('Start built-in server', 'server') while True: if sys.is_finalizing(): log_info('Shutdown built-in server', 'server') return
print(sys.hash_info) print(sys.hexversion) print(sys.implementation) print(sys.int_info) print(sys.__interactivehook__) s1 = sys.intern('hello!哈哈') s2 = 'hello!哈哈' s3 = ('hello!哈哈') print(s1 == s2) print(s1 is s3) print(sys.is_finalizing()) # 最大的Int值。32位平台是2**31 - 1。64位平台是2**63 - 1。 print(sys.maxsize) # unicode字符的最大代码点。 print(sys.maxunicode) print(chr(sys.maxunicode)) # 1114111 (0x10FFFF in hexadecimal). # print(chr(sys.maxunicode +1)) print(sys.meta_path) print(sys.modules) import timeit print(sys.modules["timeit"])
def close(self): """Close the viewer window.""" # avoid `Python is likely shutting down` ImportError raised by cocoa if not sys.is_finalizing(): super().close()
#setswitchinterval(n),在Python解释器中设置理想的线程切换延迟, #如果解释器执行长序列的不可中断代码(这是特定于实现的和依赖于工作负载的), #则切换线程的实际频率可能较低。 #该参数必须以秒为单位表示所需的切换延迟典型值为0.005(5毫秒)。 #set_coroutine_wrapper(wrapper),设置一个协程对象的装饰器 #set_asyncgen_hooks(*,firstiter=None,finalizer=None), #为异步生成器对象设置终结器。 #intern(string),python字符串的内存驻留机制, ''' python默认只会对由字符 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" 构成字符串进行intern。 由于有上面的限制,我们可以指定任意字符串使用intern机制,来减少内存占用 ''' #getallocatedblocks(),返回当前分配的内存块数量,而不管它们的大小。 print(sys.getallocatedblocks()) #getdefaultencoding(),返回Unicode实现使用的当前默认字符串编码。 print(sys.getdefaultencoding()) #getwindowsversion(),返回当前运行的windows版本的信息,以元组形式, print(sys.getwindowsversion()) #is_finalizing(),如果python已经退出则返回true sys.is_finalizing()
def my_fun() -> MyClass: sys.is_finalizing() combinations([], 2) return MyClass()
async def handle_comm(self, comm): """Dispatch new communications to coroutine-handlers Handlers is a dictionary mapping operation names to functions or coroutines. {'get_data': get_data, 'ping': pingpong} Coroutines should expect a single Comm object. """ if self.__stopped: comm.abort() return address = comm.peer_address op = None logger.debug("Connection from %r to %s", address, type(self).__name__) self._comms[comm] = op await self try: while True: try: msg = await comm.read() logger.debug("Message from %r: %s", address, msg) except OSError as e: if not sys.is_finalizing(): logger.debug( "Lost connection to %r while reading message: %s." " Last operation: %s", address, e, op, ) break except Exception as e: logger.exception("Exception while reading from %s", address) if comm.closed(): raise else: await comm.write(error_message(e, status="uncaught-error")) continue if not isinstance(msg, dict): raise TypeError( "Bad message type. Expected dict, got\n " + str(msg) ) try: op = msg.pop("op") except KeyError as e: raise ValueError( "Received unexpected message without 'op' key: " + str(msg) ) from e if self.counters is not None: self.counters["op"].add(op) self._comms[comm] = op serializers = msg.pop("serializers", None) close_desired = msg.pop("close", False) reply = msg.pop("reply", True) if op == "close": if reply: await comm.write("OK") break result = None try: if op in self.blocked_handlers: _msg = ( "The '{op}' handler has been explicitly disallowed " "in {obj}, possibly due to security concerns." ) exc = ValueError(_msg.format(op=op, obj=type(self).__name__)) handler = raise_later(exc) else: handler = self.handlers[op] except KeyError: logger.warning( "No handler %s found in %s", op, type(self).__name__, exc_info=True, ) else: if serializers is not None and has_keyword(handler, "serializers"): msg["serializers"] = serializers # add back in logger.debug("Calling into handler %s", handler.__name__) try: result = handler(comm, **msg) if inspect.isawaitable(result): result = asyncio.ensure_future(result) self._ongoing_coroutines.add(result) result = await result except (CommClosedError, CancelledError): if self.status in (Status.running, Status.paused): logger.info("Lost connection to %r", address, exc_info=True) break except Exception as e: logger.exception("Exception while handling op %s", op) if comm.closed(): raise else: result = error_message(e, status="uncaught-error") if reply and result != Status.dont_reply: try: await comm.write(result, serializers=serializers) except (OSError, TypeError) as e: logger.debug( "Lost connection to %r while sending result for op %r: %s", address, op, e, ) break self._comms[comm] = None msg = result = None if close_desired: await comm.close() if comm.closed(): break finally: del self._comms[comm] if not sys.is_finalizing() and not comm.closed(): try: comm.abort() except Exception as e: logger.error( "Failed while closing connection to %r: %s", address, e )
def critical(self, msg): tosend = str(self.env.now) + ';' + msg if sys.is_finalizing() == False: self.logger.critical(tosend)
sys.exec_prefix ) #A string giving the site-specific directory prefix where the platform-dependent Python files are installed print( sys.getallocatedblocks() ) #Return the number of memory blocks currently allocated by the interpreter, regardless of their size print(sys.hexversion) #The version number encoded as a single integer print( sys.implementation ) #An object containing information about the implementation of the currently running Python interpreter print( sys.int_info ) #A named tuple that holds information about Python’s internal representation of integers. The attributes are read only. print(sys.is_finalizing( )) #Return True if the Python interpreter is shutting down, False otherwise. print( sys.maxunicode ) # An integer giving the value of the largest Unicode code point, i.e. 1114111 (0x10FFFF in hexadecimal) print( sys.implementation ) #An object containing information about the implementation of the currently running Python interpreter print( sys.int_info ) #A named tuple that holds information about Python’s internal representation of integers.
def sys_func(): lists = sys.argv # 传递给Python脚本的命令行参数列表 => python p.py -> ['p.py'] / python p.py a 1 -> ['p.py', 'a', '1'] / 程序内执行 -> [''] strs = sys.getdefaultencoding() # 默认字符集名称 strs = sys.getfilesystemencoding() # 系统文件名字符集名称 num = sys.getrefcount(object) # 返回object的引用计数(比实际多1个) dicts = sys.modules # 已加载的模块, 可修改, 但不能通过修改返回的字典进行修改 lists = sys.path # 模块搜索路径 sys.path.append("./test") # 动态添加模块搜索路径 strs = sys.platform # 平台标识符(系统身份进行详细的检查,推荐使用) Linux:'linux' / Windows:'win32' / Cygwin:'cygwin' / Mac OS X:'darwin' strs = sys.version # python解释器版本 lists = sys.thread_info # 线程信息 num = sys.api_version # 解释器C API版本 types, value, back = sys.exc_info() # 捕获异常 详见 异常 文章的 excep() 代码块第二小部分(http://blog.csdn.net/rozol/article/details/69313164) sys.excepthook(types, value, back) # 打印异常 types = sys.last_type value = sys.last_value back = sys.last_traceback # sys.exit([arg]) // 引发SystemExit异常退出Python(可以try), 范围[0,127], None==0, "string"==1 sys.exit(0) num = sys.getrecursionlimit() # 最大递归数(堆栈最大深度), 详见 函数 文章(http://blog.csdn.net/rozol/article/details/69242050) sys.setrecursionlimit(5000) # 修改最大递归数 fnum = sys.getswitchinterval() # 获取线程切换间隔 sys.setswitchinterval(0.005) # 设置线程切换间隔, 单位秒 num = sys.getcheckinterval() # 解释器的检查间隔 sys.setcheckinterval(100) # 设置解释器检查间隔, 执行(默认)100个虚拟指令执行一次检查, 值为<=0时,检查每个虚拟指令 # sys.stdin // 标准输入流 strs = sys.stdin.readline()[:-1] # sys.stdout // 标准出入输出 sys.stdout.write(">>") sys.stdout.flush() # sys.stderr // 标注错误流 sys.stderr.write(">>") # --- lists = sys.builtin_module_names # 所有模块 (注:非导入模块) path = sys.base_exec_prefix # Python安装路径 path = sys.base_prefix # 同base_exec_prefix path = sys.exec_prefix # 同base_exec_prefix path = sys.prefix # 同base_exec_prefix path = sys.executable # Python解释器的绝对路径 strs = ys.byteorder # 本机字节顺序指示器, big-endian(最高有效字节在第一位)值为'big', little-endian(最低有效字节在第一位)值为'little' strs = sys.copyright # python版权 num = sys.hexversion # 16进制版本号 lists = sys.implementation # 当前运行的解释器的信息 num = sys.getallocatedblocks() # 解释器当前分配的内存块的数量 boolean = sys.dont_write_bytecode # 是否不会尝试导入源模块是写入.pyc文件 (False会写入.pyc文件) # sys.getsizeof(object[, default]) // 返回对象的大小bit, 只计算自身内存消耗,不计算引用对象的内存消耗, 调用对象的__sizeof__(), default没有获取到默认返回值 num = sys.getsizeof(object) boolean = sys.is_finalizing() # 解释器是否正在被关机 num = sys.maxsize # 最大整数值(2 ** 31 -1), 与系统有关 num = sys.maxunicode # 最大Unicode值的整数 (1114111) strs = sys.ps1 # 解释器主提示符 strs = sys.ps2 # 解释器次提示符 sys.call_tracing(func, ("arg",)) # 调用函数 sys._clear_type_cache() # 清除内部类型缓存 sys._debugmallocstats() # 打印CPython内存分配器状态的低级信息 sys.setprofile(profilefunc) # 设置profile函数, 默认None sys.getprofile() # 获取profile函数 sys.settrace(tracefunc) # 设置跟踪函数, def tracefunc(frame、event 和arg): sys.gettrace() # 获取跟踪函数, 默认None sys.set_coroutine_wrapper(wrapper) # 设置包装 def wrapper(coro): sys.get_coroutine_wrapper() # 包装, 默认None
def my_fun() -> MyClass: assert gather sys.is_finalizing() combinations([], 2) return variable
def __del__(self): # __del__ is called even if __init__ fails, need to detect this if hasattr(self, "_loop_runner") and not sys.is_finalizing(): self.close()