摘要:
详情参考ctypes用法,暂时没有时间研究http://docs.python.org/library/ctypes.html?highlight=ctypes#ctypes 阅读全文
posted @ 2012-09-27 22:31
gmark
阅读(339)
评论(0)
推荐(0)
摘要:
Python与C、C++交互的时候,如果进行网络消息的收发,需要讲数据打包解包为字节流。这时候就会用到Struct模块中的pack、unpack函数打包: PKG = '' # '!' means network byte #PkgHead PKG += pack('!i', 0x54434d) #int PKG += pack('!H', 4) #ushort PKG += pack('!i', calcsize('!iiHiiiiIic')) #le... 阅读全文
posted @ 2012-09-27 22:26
gmark
阅读(1282)
评论(0)
推荐(0)
摘要:
多进程的方法就是传统的fork语句,如果返回为0,则子进程创建成功,在进程中运行#在子进程fork之前,调用本方法防止僵尸进程的出现#SIGCHLD为子进程,交给系统0号进程 init去回收signal.signal(signal.SIGCHLD,signal.SIG_IGN)#如果fork的返回值为0,则说明是子进程if(0 == os.fork()): #创建一个新的守护进程 daemon = MyDaemon(pidfile) if daemon: daemon.start()守护进程和多进程结合起来,可以做到主进程根据需要去创建子守护进程执行任务,并检测... 阅读全文
posted @ 2012-09-27 22:07
gmark
阅读(1293)
评论(0)
推荐(1)
摘要:
# -*- coding: utf-8 -*-import sys, os, time, atexitfrom signal import SIGTERMclass Daemon: def __init__(self, pidfile, stderr='/data/deamon_err.log', stdout='/data/deamon_out.log', stdin='/dev/null'): self.stdin = stdin self.stdout = stdout self.stderr = stderr self.pi... 阅读全文
posted @ 2012-09-27 21:55
gmark
阅读(5638)
评论(1)
推荐(1)
摘要:
Python中有ConfigParser类,可以很方便的从配置文件中读取数据(如DB的配置,路径的配置),所以可以自己写一个函数,实现读取config配置。config文件的写法比较简单,[section]下配置key=value,一下是例子:db.conf#配置数据库[database]dbhost=127.0.0.1dbport=3366dbname=testdbuser=testdbpassword=testdbcharset=utf8接着写一个读取config的方法模块#encoding:utf-8#name:mod_config.pyimport ConfigParserimport 阅读全文
posted @ 2012-09-27 20:40
gmark
阅读(36942)
评论(0)
推荐(0)