Python & FreeBSD : threading. currentThread().ident 即使在不同的进程中也返回相同的值

标签 python multithreading freebsd

正如标题所说,对 threading.currentThread().ident 的不同调用返回 34382823872,即使在不同的进程中也是如此。 (使用 Python 3.1 和 FreeBSD)

是不是FreeBSD的python线程问题?

最佳答案

你在 REPL 中测试这个吗?还是在实际程序中?我问是因为当我使用 REPL 运行以下命令时,我得到了相同的结果,但是当我运行与脚本相同的命令时,线程具有不同的标识符。

import threading

def thid():
    print threading.currentThread().ident

t1 = threading.Thread(target=thid)
t2 = threading.Thread(target=thid)
t1.start()
t2.start()
t1.join()
t2.join()

REPL 输出:

>>> t1.start()
4301111296
>>> t2.start()
4301111296

脚本输出:

me@mine:~ $ python th.py 
4300935168
4302835712

我怀疑这是预期的行为;以下来自threading文档:

The ‘thread identifier’ of this thread or None if the thread has not been started. This is a nonzero integer. See the thread.get_ident() function. Thread identifiers may be recycled when a thread exits and another thread is created.

此外,我在 REPL 中修改了 thid 如下:

>>> def thid():
...     print threading.currentThread().ident
...     sleep(10)

当我在调用 t1.start() 的 10 秒内调用 t2.start() 时,它们有不同的 ID,但如果我等待超过 10 秒,他们有相同的ID。

如果要区分不同的线程,调用id(threading.currentThread())即可。 Python id 总是不同的。

关于Python & FreeBSD : threading. currentThread().ident 即使在不同的进程中也返回相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5688503/

相关文章:

c - 进程如何查询它何时开始?

Python xlwt - 将列整数转换为 excel 单元格引用。例如 (3,6) 到 C6

python - 句子中最常见的单词按类别分组

java - 一个线程中对非 volatile 成员变量的赋值是否保证在另一个线程中可见?

java - run() 方法后线程继续运行

c - 如何在 FreeBSD 中调试 malloc/memleaks

python - 为什么调用 cv2.VideoCapture() 会改变 v4l 相机的像素格式?

python - 如何从 dockerpy 中的 docker-run 管道标准输出

java - ConcurrentHashMap 如何处理重新散列?

mysql - 无法启动mysql服务器