質問

私は、マルチスレッドのPythonプログラムを持っている、とのメッセージに続いて、タイムスタンプを書き出すユーティリティ関数、writeLog(message)、。残念ながら、結果のログファイルには、どのメッセージが生成されたスレッドの兆候を付与するものではありません。

私はwriteLog()がそれを呼んでいるスレッドを識別するために、メッセージに何かを追加できるようにしたいと思います。もちろん、私はただのスレッドがこの情報を渡す作ることができますが、それは多くの作業になります。私が使用することができos.getpid()のいくつかのスレッド相当するものはありますか?

役に立ちましたか?

解決

threading.get_ident() の作品、または<のhref = "HTTPS :「REL = "noreferrer">(Python用又はthreading.current_thread().ident threading.currentThread().identする //docs.python.org/3/library/threading.html#threading.current_thread <2.6)

他のヒント

ロギングを使用すると、自動的に各ログエントリには、現在のスレッドの識別子を追加することができますモジュール 。 ちょうどあなたのロガーのフォーマット文字列でこれらのたLogRecord のマッピングキーのいずれかを使用します

  

の%(スレッド)D:のスレッドID(利用可能な場合)

。      

の%(のthreadName)S:のスレッド名(使用可能な場合)

と、それを使用して、デフォルトのハンドラを設定ます:

logging.basicConfig(format="%(threadName)s:%(message)s")

thread.get_ident()機能は、Linux上で長整数を返します。それは本当にスレッドIDはありません。

私はこの方法を使用します本当にLinux上のスレッドIDを取得します:

import ctypes
libc = ctypes.cdll.LoadLibrary('libc.so.6')

# System dependent, see e.g. /usr/include/x86_64-linux-gnu/asm/unistd_64.h
SYS_gettid = 186

def getThreadId():
   """Returns OS thread id - Specific to Linux"""
   return libc.syscall(SYS_gettid)

私はこのようなスレッドIDの例を見ます:

class myThread(threading.Thread):
    def __init__(self, threadID, name, counter):
        self.threadID = threadID
        ...

同様name属性を示していますのモジュールのドキュメントをスレッド:

...

A thread has a name. 
The name can be passed to the constructor, 
and read or changed through the name attribute.

...

Thread.name

A string used for identification purposes only. 
It has no semantics. Multiple threads may
be given the same name. The initial name is set by the constructor.

あなたは、現在実行中のスレッドのIDENTを得ることができます。現在のスレッドが終了した場合IDENTは、他のスレッドのために再利用することができた。

スレッド数

するときは、クレートスレッドのインスタンスを、名前がパターンであるスレッドへの暗黙の与えられています

の名前は意味を持たず、名前は一意である必要はありません。すべての実行中のスレッドのidentがユニークです。

import threading


def worker():
    print(threading.current_thread().name)
    print(threading.get_ident())


threading.Thread(target=worker).start()
threading.Thread(target=worker, name='foo').start()

関数threading.current_thread()現在実行中のスレッドを返します。このオブジェクトは、スレッドの全情報を保持します。

私はPythonで複数のスレッドを作成し、Iは、スレッドオブジェクトを印刷し、そしてIはident変数を使用してIDを印刷しました。私は、すべてのIDが同じである参照してください。

<Thread(Thread-1, stopped 140500807628544)>
<Thread(Thread-2, started 140500807628544)>
<Thread(Thread-3, started 140500807628544)>

同様に、私はOSレベルのスレッド識別子(!= thread.get_ident())を取得し、特定の番号に依存し、AMD64専用されていない以下のようなものを使用するために必要な@brucexinします

---- 8< ---- (xos.pyx)
"""module xos complements standard module os""" 

cdef extern from "<sys/syscall.h>":                                                             
    long syscall(long number, ...)                                                              
    const int SYS_gettid                                                                        

# gettid returns current OS thread identifier.                                                  
def gettid():                                                                                   
    return syscall(SYS_gettid)                                                                  

---- 8< ---- (test.py)
import pyximport; pyximport.install()
import xos

...

print 'my tid: %d' % xos.gettid()

とはいえ、これはCythonに依存します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top