發表文章

目前顯示的是 6月, 2021的文章

InfluxDB flux 自己寫移動累加function

  InfluxDB flux 自己寫移動累加function 例如:我要得知 6小時交易量 / 6小時累積雨量 等等 如何Query 不幸的 flux 內建只有移動平均 movingAverage() function timedMovingAverage() function 可以使用 那就自己寫一個 這時候就自己寫一個function 直接看語法: timedMovingSum = ( every , period , column = "_value" , tables = < - ) => tables | > window ( every : every , period : period ) | > sum ( column : column ) | > duplicate ( column : "_stop" , as : "_time" ) | > window ( every : inf ) usage from ( bucket : "quote" ) | > range ( start : v . timeRangeStart , stop : v . timeRangeStop ) | > filter ( fn : ( r ) => r . _measurement == "realtime" and r . symbol == "${symbolSel}" and r . _field == "${col}" ) | > difference ( ) | > difference ( ) | > timedMovingSum ( every : 5 m , period : 1 h ) | > movingAverage ( n : int ( v : strings . replaceAll ( v :

Python closure & late binding 陷阱

Python closure & late binding 陷阱 不囉嗦直接看code # Online Python compiler (interpreter) to run Python online. # Write Python 3 code in this online editor and run it. class TC : def __init__ ( self, condition_list: list = [ 'A' ] ): self.condition_list = condition_list def add ( self ): temp_tc = TC() self.condition_list.extend(temp_tc.condition_list) tc1 = TC() tc1.add() print(tc1.condition_list) tc2 = TC() tc2.add() print(tc2.condition_list) tc3 = TC() tc3.add() print(tc3.condition_list) 很直覺的答案應該是如下 [ 'A' , 'A' ] [ 'A' , 'A' ] [ 'A' , 'A' ] 但是結果卻是如下 [ 'A' , 'A' ] [ 'A' , 'A' , 'A' , 'A' ] [ 'A' , 'A' , 'A' , 'A' , 'A' , 'A' , 'A' , 'A' ] 結論 很明顯這有很嚴重的 memory leak 的 issue 主要是closure讓區域變數無法release, condition_list 看似已經從內存中消失了,但依然能夠訪問