In fact, the two can work together normally.
The only drawback is that logging displays such records.
2011 - 07 - 02 23 : 14 : 18 , 154 - Dummy-1 - Thread - Debug - This Is Qthread_1 output
2011 - 07 - 02 23 : 14 : 18 , 467 - Dummy-2 - Thread - Debug - This Is Qthread_2 output
2011 - 07 - 02 23 : 14 : 18 , 811 - Dummy-1 - Thread - Debug - This Is Qthread_1 output
2011 - 07 - 02 23 : 14 : 19 , 217 - Dummy-2 - Thread - Debug - This Is Qthread_2 output
# Coding: UTF-8
Import Sys
Import OS
Import Time
Import Threading
From Pyside Import Qtcore
From Pyside Import Qtgui
Import Logging
Count = 0
Lock = Threading. rlock ()
Class Qt_dummythread (threading. Thread ):
Def _ Init __ (Self ):
Threading. thread. _ Init __ (Self, name = Threading. _ newname ( " Qthread _ % d " ))
# Thread. _ block consumes an OS-level locking primitive, which
# Can never be used by a _ dummythread. Since a _ dummythread
# Instance is immortal, that's bad, so release this resource.
Del Self. _ thread _ Block
Self. _ thread _ started. Set ()
Self. _ set_ident ()
Threading. _ active_limbo_lock.acquire ()
Threading. _ active [threading. _ get_ident ()] = Self
Threading. _ active_limbo_lock.release ()
Def _ Set_daemon (Self ):
Return True
Def Join (self, timeout = None ):
Assert False, " Cannot join a dummy thread "
Threading. _ dummythread = Qt_dummythread
Def Getthreadname ():
Global Count
With lock:
Count + = 1
Return STR (count)
Class Thread (qtcore. qthread ):
Def _ Init __ (Self ):
Super (thread, self ). _ Init __ ()
Self. _ th_name = ""
Def Run (self, * ARGs, ** Kwargs ):
Self. _ th_name = Threading. currentthread (). Name
Import Random
Logger = Logging. getlogger ( " Thread " )
For I In Range ( 10 ):
Time. Sleep (random. Random ())
Logger. debug (
" This is {0} output " . Format (
Self. _ th_name,
)
)
Return
Def Initiallogging ():
Logger = Logging. getlogger ()
Logger. setlevel (logging. Debug)
# Create console handler and set level to debug
Ch = Logging. streamhandler (SYS. stdout)
Ch. setlevel (logging. Debug)
# Create formatter
Formatter = Logging. formatter ( ' % (Asctime) S-% (threadname)-12 S-% (name)-12 S-% (levelname)-8 s-% (Message) S ' )
Ch. setformatter (formatter)
Logger. addhandler (CH)
Class MW (qtgui. qdialog ):
Def _ Init __ (Self ):
Super (MW, self ). _ Init __ ()
Layout = Qtgui. qvboxlayout ()
BTN = Qtgui. qpushbutton (Self)
BTN. settext ( " Click " )
Self. setlayout (layout)
Layout. addwidget (BTN)
BTN. clicked. Connect (self. onbtn)
Self. t = []
Def Onbtn (Self ):
T = Thread ()
T. Finished. Connect (self. onthreadend)
T. Terminated. Connect (self. onthreadend)
Self. T. append (t)
T. Start ()
Def Onthreadend (Self ):
For I In Self. T:
If I. terminated:
I. Wait ()
Self. T. Remove (I)
Def Closeevent (self, * ARGs, ** Kwargs ):
For I In Self. T:
I. Exit ()
I. Wait ()
Return Qtgui. qdialog. closeevent (self, * ARGs, ** Kwargs)
Def Main ():
Initiallogging ()
App = Qtgui. qapplication ([])
MW = MW ()
MW. Show ()
App.exe C _()
If _ Name __ = ' _ Main __ ' :
Main ()
The following is the display after applying the hack.
2011 - 07 - 02 23 : 14 : 18 , 154 - Qthread_1 - Thread - Debug - This Is Qthread_1 output
2011 - 07 - 02 23 : 14 : 18 , 467 - Qthread_2 - Thread - Debug - This Is Qthread_2 output
2011 - 07 - 02 23 : 14 : 18 , 811 - Qthread_1 - Thread - Debug - This Is Qthread_1 output
2011 - 07 - 02 23 : 14 : 19 , 217 - Qthread_2 - Thread - Debug - This Is qthread_2 output