Posts

Showing posts from March, 2010

[python] 關於thread的timer

寫MT實在很惱人,特別是讓threads定期做些事情時,一不小心就busy loop... 先前的做法都是使用os內建的sleep(),  但缺點是無法掌握程式結束的時間。 前幾天偉任建議使用threading模組內建的Timer class 看了做法後,它是使用Event()做為timer,  然後每次invoke一個新的thread去執行該function (其實BT源碼就是這樣做Timer了,只能說當時太弱,不懂期間的差異) Results: import  threading import  thread   mutex = thread. allocate_lock ( ) def printm ( msg ) :      with  mutex:          print   "[%s]"  % threading. current_thread ( ) . name , "say:" , msg     class  MyThread ( threading. Thread ) :     def __init__ ( self,  name ) :         threading. Thread .__init__ ( self )         self. name  =  name         self. timer  = threading. Event ( )         def do_dialog ( self ) :         printm ( "How do you do?!" )         self. setDialogTimer ( )         def setDialogTimer ( self ) :         self. timer . wait ( 3 )          if  not self. timer . is_set ( ) :             self. do_dialog ( )                             def cancelDialogTimer ( self ) :         self. timer . set ( )             def run

[python] 隱藏 console

python 要如何做到隱藏console 這個應該是屬於windows的問題吧 因為作業系統是xp 所以去找了 win32api and python相關 果然找到 前人的解法 Python代码  import  win32api, win32gui   ct = win32api.GetConsoleTitle()   hd = win32gui.FindWindow( 0 ,ct)   win32gui.ShowWindow(hd, 0 )   很简单吧,就是获得console的handle,然后隐藏(0分别代表NULL和SW_HIDE)。启动还是用python.exe。 不過在啟動前還是得先去下載 pywin32api  (我是用py26), 不然python無法找到這兩個module 唉呀越寫越上層的話,若上面的AP沒有提供解法,底層實做原理又不懂的話,是很惱人的...