1+ from PyFlow .Core import NodeBase
2+ from PyFlow .Core .NodeBase import NodePinsSuggestionsHelper
3+ from PyFlow .Core .Common import *
4+
5+ import threading
6+
7+ import time
8+
9+ @SingletonDecorator
10+ class singletonThread ():
11+ isRunning = False
12+ instanceCount = 0
13+ def __init__ (self ):
14+ self .Runner = threading .Thread (target = self .run_loop , daemon = True )
15+ self .value = 0
16+ if not self .isRunning :
17+ self .Runner .start ()
18+ self .isRunning = True
19+
20+ def run_loop (self ):
21+ self .isRunning = True
22+ while self .isRunning :
23+ time .sleep (0.1 )
24+ print ("running" )
25+ self .value += 1
26+
27+ def cleanUp (self ):
28+ print (self .instanceCount )
29+ self .instanceCount -= 1
30+ if self .instanceCount == 0 :
31+ self .isRunning = False
32+ self .Runner .join ()
33+ del self
34+ print ("cleanUp" )
35+
36+ class singletonThreadSampleNode (NodeBase ):
37+ def __init__ (self , name ):
38+ super (singletonThreadSampleNode , self ).__init__ (name )
39+ self .singletonThread = None
40+ self .value = self .createOutputPin ('value' , 'IntPin' )
41+
42+ def postCreate (self , jsonTemplate = None ):
43+ self .singletonThread = singletonThread ()
44+ if not self .singletonThread .isRunning :
45+ self .singletonThread .Runner = threading .Thread (target = self .singletonThread .run_loop , daemon = True )
46+ self .singletonThread .Runner .start ()
47+ self .bCacheEnabled = False
48+ super (singletonThreadSampleNode , self ).postCreate (jsonTemplate = jsonTemplate )
49+
50+ @staticmethod
51+ def pinTypeHints ():
52+ helper = NodePinsSuggestionsHelper ()
53+ helper .addInputDataType ('StringPin' )
54+ helper .addOutputDataType ('AnyPin' )
55+ helper .addInputStruct (StructureType .Single )
56+ helper .addOutputStruct (StructureType .Dict )
57+ return helper
58+
59+ @staticmethod
60+ def category ():
61+ return 'Interactive singletonThreads'
62+
63+ @staticmethod
64+ def keywords ():
65+ return []
66+
67+ @staticmethod
68+ def description ():
69+ return "Get singletonThreadSampleNode Infos."
70+
71+ def kill (self , * args , ** kwargs ):
72+ print ("deleting singletonThreadSampleNode" )
73+ self .singletonThread .cleanUp ()
74+ super (singletonThreadSampleNode , self ).kill (* args , ** kwargs )
75+
76+ def compute (self , * args , ** kwargs ):
77+ if not self .singletonThread .isRunning :
78+ self .singletonThread .Runner = threading .Thread (target = self .singletonThread .run_loop , daemon = True )
79+ self .singletonThread .Runner .start ()
80+ self .value .setData (self .singletonThread .value )
0 commit comments