Example #1
0
    def test_threads_scope(self):
        class Foo:
            def bar(self):
                return 'baz'

        with s_threads.ScopeLocal(foo=Foo()):
            foo = s_threads.local('foo')
            self.assertEqual(foo.bar(), 'baz')

        self.assertIsNone(s_threads.local('foo'))
Example #2
0
    def test_threads_scope(self):

        class Foo:
            def bar(self):
                return 'baz'

        with s_threads.ScopeLocal(foo=Foo()):
            foo = s_threads.local('foo')
            self.assertEqual( foo.bar(), 'baz' )

        self.assertIsNone( s_threads.local('foo') )
Example #3
0
    def iAmSynSvc(self, iden, props):
        '''
        API used by synapse service to register with the bus.

        Example:

            sbus.iAmSynSvc('syn.blah', foo='bar', baz=10)

        '''
        props['iden'] = iden

        svcfo = (iden,props)

        sock = s_threads.local('sock')
        if sock != None:
            def onfini():
                oldsvc = self.services.pop(iden,None)
                self.bytag.pop(iden)
                self.fire('syn:svc:fini', svcfo=oldsvc)

            sock.onfini(onfini)
            
        self.services[iden] = svcfo

        tags = props.get('tags',())

        self.bytag.put(iden,tags)

        self.fire('syn:svc:init', svcfo=svcfo)
Example #4
0
    def iAmSynSvc(self, iden, props):
        '''
        API used by synapse service to register with the bus.

        Example:

            sbus.iAmSynSvc('syn.blah', foo='bar', baz=10)

        '''
        props['iden'] = iden

        svcfo = (iden, props)

        sock = s_threads.local('sock')
        if sock != None:

            def onfini():
                oldsvc = self.services.pop(iden, None)
                self.bytag.pop(iden)
                self.fire('syn:svc:fini', svcfo=oldsvc)

            sock.onfini(onfini)

        self.services[iden] = svcfo

        tags = props.get('tags', ())

        self.bytag.put(iden, tags)

        self.fire('syn:svc:init', svcfo=svcfo)
Example #5
0
    def wait(self, jid, timeout=None):
        '''
        Wait for a job to complete by id.

        Example:

            boss.wait( jid, timeout=10 )

        '''
        s_threads.iWillWait()

        if timeout == None:
            timeout = s_threads.local('syntimeout')

        with self.joblock:
            job = self._boss_jobs.get(jid)
            if job == None:
                return True

            joblocal = self.joblocal.get(jid)
            evt = joblocal.get('waitevt')
            if evt == None:
                evt = threading.Event()
                joblocal['waitevt'] = evt

        evt.wait(timeout=timeout)
        return evt.is_set()
Example #6
0
    def wait(self, jid, timeout=None):
        '''
        Wait for a job to complete by id.

        Example:

            boss.wait( jid, timeout=10 )

        '''
        s_threads.iWillWait()

        if timeout == None:
            timeout = s_threads.local('syntimeout')

        with self.joblock:
            job = self._boss_jobs.get(jid)
            if job == None:
                return True

            joblocal = self.joblocal.get(jid)
            evt = joblocal.get('waitevt')
            if evt == None:
                evt = threading.Event()
                joblocal['waitevt'] = evt

        evt.wait(timeout=timeout)
        return evt.is_set()
Example #7
0
    def iAmHive(self, iden, **info):
        hive = (iden,info)
        self.hives[iden] = hive

        sock = s_threads.local('sock')
        def onfini():
            self.fireHiveFini(iden)
        sock.onfini( onfini )

        self.fire('hive:hive:init', hive=hive)
Example #8
0
    def iAmDrone(self, iden, **info):

        drone = (iden,info)
        self.drones[iden] = drone

        sock = s_threads.local('sock')
        def onfini():
            self.fireDroneFini(iden)
        sock.onfini(onfini)

        self.fire('hive:drone:init', iden=iden)
Example #9
0
    def iAmHive(self, iden, **info):
        hive = (iden, info)
        self.hives[iden] = hive

        sock = s_threads.local('sock')

        def onfini():
            self.fireHiveFini(iden)

        sock.onfini(onfini)

        self.fire('hive:hive:init', hive=hive)
Example #10
0
    def iAmDrone(self, iden, **info):

        drone = (iden, info)
        self.drones[iden] = drone

        sock = s_threads.local('sock')

        def onfini():
            self.fireDroneFini(iden)

        sock.onfini(onfini)

        self.fire('hive:drone:init', iden=iden)