Python守護進程(多線程開發)

來源:互聯網
上載者:User
#!/usr/bin/pythonimport sys,time,json,loggingimport Queue, threading, datetimefrom lib.base.daemon import Daemonfrom lib.queue.httpsqs.HttpsqsClient import HttpsqsClientfrom lib.db.DbMongodb import DbMongodblogging.basicConfig(level=logging.DEBUG,                format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',                datefmt='%a, %d %b %Y %H:%M:%S',                filename='myapp.log',                filemode='w')                 queue = Queue.Queue()       httpsqs = HttpsqsClient('192.168.0.218','1218','httpsqs.com')db = DbMongodb('192.168.0.119','testdb')         class ThreadGetHttpSqs(threading.Thread):    def __init__(self):        threading.Thread.__init__(self)        self.httpsqs = httpsqs        self.queue = queue         def run(self):        while True:            data = self.httpsqs.get('logtest')            if data is not None:                self.queue.put(data)                logging.info('get:id %s , tablename %s' % (self.getName(),data))            else:                time.sleep(3)                                       class ThreadInsertDB(threading.Thread):    def __init__(self):        threading.Thread.__init__(self)        self.queue = queue        self.db = db             def run(self):        while True:            chunk = self.queue.get()            s = json.loads(chunk)            tablename = s['table']            data = s['data']            self.db.save(tablename,data)            logging.info('insert:id %s , tablename %s' % (self.getName(),tablename))            self.queue.task_done()             class MyDaemon(Daemon):    def _run(self):        while True:            for i in range(2):                t = ThreadGetHttpSqs()                #t.setDaemon(True)                t.start()                         for i in range(2):                b = ThreadInsertDB()                #t.setDaemon(True)                b.start()            #線程已經為永真迴圈,進程不能再迴圈            time.wait()                                              if __name__ == "__main__":    daemon = MyDaemon('/tmp/daemon-example.pid')    if len(sys.argv) == 2:        if 'start' == sys.argv[1]:            daemon.start()        elif 'stop' == sys.argv[1]:            daemon.stop()        elif 'restart' == sys.argv[1]:            daemon.restart()        else:            print "Unknown command"            sys.exit(2)        sys.exit(0)    else:        print "usage: %s start|stop|restart" % sys.argv[0]        sys.exit(2)
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.