Exemple #1
0
 def _monitorScan(self):
     def run():
         while self.proc.poll() is None:
             sleep(0.5)
         self.onScanComplete(self._savePath())
             
     cmds = [(run, [], {})]
     callMultipleInThread(cmds)
Exemple #2
0
def __expire_notify(mk_db):
    def _getpage(url):
        errback = lambda x:log.err(x,'getPage error')
        d = getPage(url)
        d.addErrback(errback)
        
    log.msg("start expire notify task..")
    db = mk_db()
    try:
        _ndays = db.query(models.SlcParam.param_value).filter_by(
            param_name = 'expire_notify_days').scalar()

        notify_tpl = db.query(models.SlcParam.param_value).filter_by(
            param_name = 'expire_notify_tpl').scalar()

        notify_url = db.query(models.SlcParam.param_value).filter_by(
            param_name = 'expire_notify_url').scalar()

        _now = datetime.datetime.now()
        _date = (datetime.datetime.now() + datetime.timedelta(days=int(_ndays))).strftime("%Y-%m-%d")
        expire_query = db.query(
            models.SlcRadAccount.account_number,
            models.SlcRadAccount.expire_date,
            models.SlcMember.email,
            models.SlcMember.mobile
        ).filter(
            models.SlcRadAccount.member_id == models.SlcMember.member_id,
            models.SlcRadAccount.expire_date <= _date,
            models.SlcRadAccount.expire_date >= _now.strftime("%Y-%m-%d"),
            models.SlcRadAccount.status == 1
        )

        log.msg('expire_notify total: %s'%expire_query.count())
        commands = []
        for account,expire,email,mobile in expire_query:
            ctx = notify_tpl.replace('#account#',account)
            ctx = ctx.replace('#expire#',expire)
            topic = ctx[:ctx.find('\n')]
            commands.append( (mail.sendmail,[email,topic,ctx],{}) )
            
            url = notify_url.replace('{account}',account)
            url = url.replace('{expire}',expire)
            url = url.replace('{email}',email)
            url = url.replace('{mobile}',mobile)
            url = url.encode('utf-8')
            url = quote(url,":?=/&")
            commands.append( (_getpage,[url],{}) )

        threads.callMultipleInThread(commands)
        log.msg("expire_notify to ansync task")

    except Exception as err:
        db.rollback()
        log.err(err,'expire notify erro')
    finally:
        db.close()
Exemple #3
0
def __expire_notify(mk_db):
    def _getpage(url):
        errback = lambda x:log.err(x,'getPage error')
        d = getPage(url)
        d.addErrback(errback)
        
    log.msg("start expire notify task..")
    db = mk_db()
    try:
        _ndays = db.query(models.SlcParam.param_value).filter_by(
            param_name = 'expire_notify_days').scalar()

        notify_tpl = db.query(models.SlcParam.param_value).filter_by(
            param_name = 'expire_notify_tpl').scalar()

        notify_url = db.query(models.SlcParam.param_value).filter_by(
            param_name = 'expire_notify_url').scalar()

        _now = datetime.datetime.now()
        _date = (datetime.datetime.now() + datetime.timedelta(days=int(_ndays))).strftime("%Y-%m-%d")
        expire_query = db.query(
            models.SlcRadAccount.account_number,
            models.SlcRadAccount.expire_date,
            models.SlcMember.email,
            models.SlcMember.mobile
        ).filter(
            models.SlcRadAccount.member_id == models.SlcMember.member_id,
            models.SlcRadAccount.expire_date <= _date,
            models.SlcRadAccount.expire_date >= _now.strftime("%Y-%m-%d"),
            models.SlcRadAccount.status == 1
        )

        syslog.info('expire_notify total: %s'%expire_query.count())
        commands = []
        for account,expire,email,mobile in expire_query:
            ctx = notify_tpl.replace('#account#',account)
            ctx = ctx.replace('#expire#',expire)
            topic = ctx[:ctx.find('\n')]
            commands.append( (mail.sendmail,[email,topic,ctx],{}) )
            
            url = notify_url.replace('{account}',account)
            url = url.replace('{expire}',expire)
            url = url.replace('{email}',email)
            url = url.replace('{mobile}',mobile)
            url = url.encode('utf-8')
            url = quote(url,":?=/&")
            commands.append( (_getpage,[url],{}) )

        threads.callMultipleInThread(commands)
        log.msg("expire_notify to ansync task")

    except Exception as err:
        db.rollback()
        syslog.error('expire notify error %s' % str(err))
    finally:
        db.close()
    def notify_deletion(self, resource):
        """
        Finds the observers that must be notified about the delete of the observed resource
        and invoke the notification procedure in different threads.

        :param resource: the node resource deleted
        """
        commands = self._observe_layer.notify_deletion(resource)
        if commands is not None:
            threads.callMultipleInThread(commands)
    def remove_observers(self, node):
        """
        Remove all the observers of a resource and and invoke the notification procedure in different threads.

        :type node: coapthon2.utils.Tree
        :param node: the node which has the deleted resource
        """
        commands = self._observe_layer.remove_observers(node)
        if commands is not None:
            threads.callMultipleInThread(commands)
Exemple #6
0
 def testCallMultiple(self):
     L = []
     N = 10
     d = defer.Deferred()
     def finished():
         self.assertEquals(L, range(N))
         d.callback(None)
     threads.callMultipleInThread([
         (L.append, (i,), {}) for i in xrange(N)
         ] + [(reactor.callFromThread, (finished,), {})])
     return d
Exemple #7
0
def main():
    f = EchoFactory()
    reactor.connectTCP("localhost", 8000, f)
    # run both methods sequentially in a thread
    commands = [(aSillyBlockingMethodOne, ["Calling First"], {})]
    commands.append((aSillyBlockingMethodTwo, ["And the second"], {}))
    threads.callMultipleInThread(commands)
    # reactor.callInThread(aSillyBlockingMethodOne, "2 seconds have passed")
    reactor.run()
    # reactor.run(installSignalHandlers=0)
    print("RUNNING")
    def notify_deletion(self, node):
        """
        Finds the observers that must be notified about the delete of the observed resource
        and invoke the notification procedure in different threads.

        :type node: coapthon2.utils.Tree
        :param node: the node which has the deleted resource
        """
        commands = self._observe_layer.notify(node)
        if commands is not None:
            threads.callMultipleInThread(commands)
Exemple #9
0
    def __init__(self):
        self.handledRoomInfo = False

        self.connection = network.gameClientFactory(self)
        self.on = True

        GAME_IP = "localhost"
        GAME_PORT = int(raw_input("Port:"))
        threads.callMultipleInThread([(self.mainGame, [], {})])

        reactor.connectTCP(GAME_IP, GAME_PORT,
                           self.connection)  # @UndefinedVariable
        reactor.run()  # @UndefinedVariable
Exemple #10
0
    def testCallMultiple(self):
        L = []
        N = 10
        d = defer.Deferred()

        def finished():
            self.assertEquals(L, range(N))
            d.callback(None)

        threads.callMultipleInThread([
            (L.append, (i,), {}) for i in xrange(N)
            ] + [(reactor.callFromThread, (finished,), {})])
        return d
Exemple #11
0
 def run(self):
     """
     Run the process, start the poller.
     """
     self.init_db()
     ip_range = self._get_range()
     while True:
         for server_ip in ip_range:
             logging.info("testing {0}".format(server_ip))
             commands = [(self.update_db_entry, [server_ip], {})]
             #self.update_db_entry(server_ip)
         threads.callMultipleInThread(commands)
         reactor.run()
         time.sleep(self.interval)
Exemple #12
0
 def run(self):
     """
     Run the process, start the poller.
     """
     self.init_db()
     ip_range = self._get_range()
     while True:
         for server_ip in ip_range:
             logging.info("testing {0}".format(server_ip))
             commands = [(self.update_db_entry, [server_ip], {})]
             #self.update_db_entry(server_ip)
         threads.callMultipleInThread(commands)
         reactor.run()
         time.sleep(self.interval)
Exemple #13
0
    def initial():
        ModelView.g.Name = "df"
        ModelView.g.Password = "******"
        z = EchoClientFactory(ModelView.g)
        commands = [(ModelView.aSillyBlockingMethodOne, ["Calling First"], {})]
        commands.append(
            (ModelView.aSillyBlockingMethodTwo, ["And the second"], {}))
        # commands.append((ModelView.aSillyBlockingMethodThree, ["And the Three"], {}))
        threads.callMultipleInThread(commands)
        #threads.deferToThread(ModelView.aSillyBlockingMethodThree,1)
        # reactor.callFromThread(ModelView.aSillyBlockingMethodThree, 1)

        reactor.callFromThread(ModelView.aSillyBlockingMethodThree, 1)
        reactor.connectTCP("localhost", 777, ModelView.z)
        reactor.run()
Exemple #14
0
    def test_callMultiple(self):
        """
        L{threads.callMultipleInThread} calls multiple functions in a thread.
        """
        L = []
        N = 10
        d = defer.Deferred()

        def finished():
            self.assertEqual(L, list(range(N)))
            d.callback(None)

        threads.callMultipleInThread([
            (L.append, (i,), {}) for i in xrange(N)
            ] + [(reactor.callFromThread, (finished,), {})])
        return d
Exemple #15
0
    def testCallMultiple(self):
        c = Counter()
        # we call the non-thread safe method, but because they should
        # all be called in same thread, this is ok.
        commands = [(c.add, (), {})] * 1000
        threads.callMultipleInThread(commands)

        when = time.time()
        oldIndex = 0
        while c.index < 1000:
            assert oldIndex <= c.index
            self.failIf(c.problem, "threads reported overlap")
            if c.index > oldIndex:
                when = time.time() # reset each count
            else:
                if time.time() > when + 5:
                    if c.index > 0:
                        self.fail("threads lost a count")
                    else:
                        self.fail("threads never started")
            oldIndex = c.index
        
        self.assertEquals(c.index, 1000)
Exemple #16
0
    def __init__(self):
        self.connection = connection.Gamefactory(self)

        self.iId = 1
        self.rooms = []

        for x in range(5):
            self.add_game("Sala " + str(x + 1), randomcolor())

        self.on = True

        threads.callMultipleInThread([(self.commands_loop, [], {})])

        self.loop(1)
        port = 10000
        while True:
            port -= 1
            try:
                reactor.listenTCP(port, self.connection)  # @UndefinedVariable
                break
            except:
                pass
        print "Info: server has started, connected to port " + str(port)
        reactor.run()  # @UndefinedVariable
Exemple #17
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from twisted.internet import reactor, threads

def dns_lookup(foo):
	print "foo"

commands = [(dns_lookup, ['foo'], {})]

threads.callMultipleInThread(commands)
reactor.run()


		
Exemple #18
0
        app.exec_()
        p.kill()
        p.wait()

    elif sys.argv[1] == "sit-down":

        root = AllOfTheStreams()
        site = Site(root)
        reactor.listenTCP(SVC_PORT, site)  #, interface='0.0.0.0')

        interlace = InterLace(root)

        cmds = [(run_forever, [interlace], {}), (reactor.stop, [], {})
                ]  # will stop twisted service if run_forever fails
        #(reactor.callFromThread, [reactor.stop], {})]
        threads.callMultipleInThread(cmds)

        reactor.run()

    elif sys.argv[1] == "headless":
        # Subprocess CouchDB, but don't make a GUI
        p = psychotherapist.init(basedir, exepath, name=APPNAME, port=PORT)
        print "%s is running on port %d" % (APPNAME, PORT)
        p.wait()

    elif sys.argv[1] == "console":
        p = psychotherapist.init(basedir, exepath, name=APPNAME, port=PORT)
        creds = psychotherapist.get_psychotherapist_creds(
            os.path.join(os.getenv("HOME"), ".freud", APPNAME, "conf"))

        root = AllOfTheStreams()
Exemple #19
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from twisted.internet import reactor, threads


def dns_lookup(foo):
    print "foo"


commands = [(dns_lookup, ['foo'], {})]

threads.callMultipleInThread(commands)
reactor.run()
        app.exec_()
        p.kill()
        p.wait()

    elif sys.argv[1] == "sit-down":

        root = AllOfTheStreams()
        site = Site(root)
        reactor.listenTCP(SVC_PORT, site)#, interface='0.0.0.0')

        interlace = InterLace(root)

        cmds = [(run_forever, [interlace], {}),
                (reactor.stop, [], {})] # will stop twisted service if run_forever fails
                #(reactor.callFromThread, [reactor.stop], {})]
        threads.callMultipleInThread(cmds)

        reactor.run()

    elif sys.argv[1] == "headless":
        # Subprocess CouchDB, but don't make a GUI
        p = psychotherapist.init(basedir, exepath, name=APPNAME, port=PORT)
        print "%s is running on port %d" % (APPNAME, PORT)
        p.wait()

    elif sys.argv[1] == "console":
        p = psychotherapist.init(basedir, exepath, name=APPNAME, port=PORT)
        creds = psychotherapist.get_psychotherapist_creds(os.path.join(os.getenv("HOME"), ".freud", APPNAME, "conf"))

        root = AllOfTheStreams()
        interlace = InterLace(root, server_uri="http://%slocalhost:%d" % (creds, PORT), basedir=basedir)
Exemple #21
0
 def start_connection(self):
     threads.callMultipleInThread([(self.commands_loop, [], {})])
Exemple #22
0
        # throttle so twitch accepts us
        sleep(5)

def shutdown():
    # stop handler, close sockets
    WooferHandler.stop()
    for (bot, tcp) in bots:
        bot.irc.quit()
        tcp.disconnect()
    reactor.stop()


if __name__ == '__main__':
    # load config from json
    config.load()

    # register the thread func that handles all dispatched commands
    WooferHandler.start()

    # register keyboard handler, when it's down call shutdown()
    threads.callMultipleInThread([(keyboard_handler, [], {}), (shutdown, [], {})])

    # open sockets once we start the message loop
    reactor.callInThread(create_bots)

    # run bots
    reactor.run()

    # save settings
    config.save()