As the title says, different calls to threading.currentThread().ident
returns 34382823872, even in different processes.开发者_运维百科 (Using Python 3.1 & FreeBSD)
Is it FreeBSD's problem with python threads or not?
Are you testing this in the REPL? Or in an actual program? I ask because when I ran the below using the REPL, I got the same result, but when I ran the same as a script, the threads had different identifiers.
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 output:
>>> t1.start()
4301111296
>>> t2.start()
4301111296
Script output:
me@mine:~ $ python th.py
4300935168
4302835712
I suspect this is expected behavior; the below is from the threading
docs:
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.
Furthermore, I modified thid
in the REPL like this:
>>> def thid():
... print threading.currentThread().ident
... sleep(10)
And when I called t2.start()
within 10 seconds of calling t1.start()
, they had different ids, but if I waited more than 10 seconds, they had the same id.
If you want to distinguish between different threads, just call id(threading.currentThread())
. Python ids are always distinct.
精彩评论