1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/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()
            = 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 in range(2):
                = ThreadGetHttpSqs()
                #t.setDaemon(True)
                t.start()
              
            for in range(2):
                = 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)

http://www.qytang.com/
http://www.qytang.com/cn/list/29/
http://www.qytang.com/cn/list/28/446.htm
http://www.qytang.com/cn/list/28/445.htm
http://www.qytang.com/cn/list/28/444.htm
http://www.qytang.com/cn/list/28/442.htm
http://www.qytang.com/cn/list/28/440.htm
http://www.qytang.com/cn/list/28/437.htm
http://www.qytang.com/cn/list/28/435.htm
http://www.qytang.com/cn/list/28/434.htm
http://www.qytang.com/cn/list/28/433.htm
http://www.qytang.com/cn/list/28/431.htm
http://www.qytang.com/cn/list/28/328.htm
http://www.qytang.com/cn/list/28/326.htm
http://www.qytang.com/cn/list/28/429.htm

Python守护进程(多线程开发)-乾颐堂的更多相关文章

  1. 开发中常遇到的Python陷阱和注意点-乾颐堂

    最近使用Python的过程中遇到了一些坑,例如用datetime.datetime.now()这个可变对象作为函数的默认参数,模块循环依赖等等. 在此记录一下,方便以后查询和补充. 避免可变对象作为默 ...

  2. python高性能编程方法一-乾颐堂

    阅读 Zen of Python,在Python解析器中输入 import this. 一个犀利的Python新手可能会注意到"解析"一词, 认为Python不过是另一门脚本语言. ...

  3. 使用python把图片存入数据库-乾颐堂

    一般情况下我们是把图片存储在文件系统中,而只在数据库中存储文件路径的,但是有时候也会有特殊的需求:把图片二进制存入数据库. 今天我们采用的是python+mysql的方式 MYSQL 是支持把图片存入 ...

  4. python 多继承详解-乾颐堂

    1 2 3 4 5 6 7 8 9 10 class A(object):    # A must be new-style class    def __init__(self):     prin ...

  5. 用 python 实现各种排序算法-乾颐堂

    总结了一下常见集中排序的算法 归并排序 归并排序也称合并排序,是分治法的典型应用.分治思想是将每个问题分解成个个小问题,将每个小问题解决,然后合并. 具体的归并排序就是,将一组无序数按n/2递归分解成 ...

  6. python时间处理详解-乾颐堂

    1.获取当前时间的两种方法: import datetime,time now = time.strftime("%Y-%m-%d %H:%M:%S") print now now ...

  7. python 守护进程 daemon

    python 守护进程 daemon # -*-coding:utf-8-*- import sys, os '''将当前进程fork为一个守护进程 注意:如果你的守护进程是由inetd启动的,不要这 ...

  8. Python守护进程和脚本单例运行

    Python 守护进程 守护进程简介 进程运行有时候需要脱离当前运行环境,尤其是Linux和Unix环境中需要脱离Terminal运行,这个时候就要用到守护进程.守护进程可以脱离当前环境要素来执行,这 ...

  9. Python实例浅谈之五Python守护进程和脚本单例运行

    一.简介 守护进程最重要的特性是后台运行:它必须与其运行前的环境隔离开来,这些环境包括未关闭的文件描述符.控制终端.会话和进程组.工作目录以及文件创建掩码等:它可以在系统启动时从启动脚本/etc/rc ...

  10. 乾颐堂7月HCIE、CCIE通过名单

    拼多多都上市了,现在很多培训机构也流行公用一张PASS了,山寨总是山寨的,不脚踏实地总是欺骗自己7月(自然月)乾颐堂通过22名学员,每个考试日通过一名HCIE.CCIE 转载于:https://blo ...

随机推荐

  1. STL 算法罗列 (转)

    非修改性序列操作(12个) 循环 for_each() 对序列中的每个元素执行某操作 查找 find() 在序列中找出某个值的第一次出现的位置 find_if() 在序列中找出符合某谓词的第一个元素 ...

  2. Jenkins的pipeline

    说明 再官网文档上有说明pipeline的工作流程,具体可以参考pipeline文档和语法文档. 这里将pipeline的生命周期图贴出来  我们暂时不会用到完整的流程,只摘取有用的部分,完成最小的流 ...

  3. 可复用的自定义Adapter

    public abstract class MyAdapter<T> extends BaseAdapter { private ArrayList<T> mData; pri ...

  4. 一个for循环写出9*9乘法表

    public class ClassTest03 { public static void main(String[] args) { for(int i=1,j=1;j<=9;j++){ if ...

  5. PdfPCell对齐方式,边框,边框颜色的使用 (转)

    原文:http://www.cnblogs.com/LifelongLearning/archive/2011/06/22/2086802.html PdfPTable和PdfPCell对象,我们可以 ...

  6. npm 自身的升级

    npm 是随 NodeJS 一起发布的包管理工具,默认采用的并不一定是最新版本,某些情况下(比如使用了某个IDE)需要最新版本的 npm , 咋办?单独升级,官方的几种说法的操作比较复杂,本经验就是说 ...

  7. 3dmax卡通渲染插件pencil+渲染线框

    转自:http://www.cr173.com/soft/179512.html http://www.psoft.co.jp/jp/ 官网和YTB有 2代的视频教程,平均每个2分钟长,无解说,是日文 ...

  8. [置顶] apt-get update 更新失败----如何清除破损而且陈旧的PPA仓库 (W: Failed to fetch http://ppa.launchpad.net/jonathonf/pyt)

    我使用sudo apt-get update之后,更新失败,遇到如下错误. W: Failed to fetch http://ppa.launchpad.net/jonathonf/python-3 ...

  9. canvas之画一个三角形

    <canvas id="canvas" width="500" height="500" style="background ...

  10. “microsoft ace oledb 12.0 未注册”疑云

    1. 有人说: 2015也是要安装32位的AccessDataengine,anycpu选32位优先才行,不然就是Microsoft.ACE.OLEDB.12.0未注册. hanstom,一个老调重弹 ...