Python daemon (multi-threaded development)
#!/usr/bin/python import sys,time,json,logging import Queue, threading, datetime from lib.base.daemon import Daemon from lib.queue.httpsqs.HttpsqsClient import HttpsqsClient from lib.db.DbMongodb import DbMongodb logging.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)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Fastapi ...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

Using python in Linux terminal...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
