mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Return the kernel thread id (gettid / pthread_threadid_np / GetCurrentThreadId). threading.get_native_id and Thread.native_id follow once the function exists. Assisted-by: Grok:4.6
25 lines
635 B
Python
25 lines
635 B
Python
"""_thread.get_native_id is the kernel thread id and changes after fork."""
|
|
|
|
import _thread
|
|
import os
|
|
|
|
nid = _thread.get_native_id()
|
|
assert isinstance(nid, int) and nid > 0
|
|
assert _thread.get_ident() != 0
|
|
|
|
if hasattr(os, "fork"):
|
|
r, w = os.pipe()
|
|
pid = os.fork()
|
|
if pid == 0:
|
|
os.write(w, str(_thread.get_native_id()).encode())
|
|
os._exit(0)
|
|
os.close(w)
|
|
child_nid = int(os.read(r, 64).decode())
|
|
os.close(r)
|
|
_, status = os.waitpid(pid, 0)
|
|
assert os.WIFEXITED(status) and os.WEXITSTATUS(status) == 0, status
|
|
assert child_nid != nid, (nid, child_nid)
|
|
assert child_nid > 0
|
|
|
|
print("ok")
|