Python监视进程
由subprocess创建一个进程,然后进行监视
每一秒钟查看一次,如果正在运行,打印pid和running,如果已停止,,继续执行任务并打印Termined
shell和stdout均设置为False
也许这对做病毒的守护进程很好
#!/usr/bin/env python
import subprocess , sys , time
p=subprocess.Popen(['ping','127.0.0.1','-n','10'], shell=False,stdout=False)
while 1:
time.sleep(1)
ret=subprocess.Popen.poll(p)
if ret is None:
print p.pid,"running"
else:
print "Termined!"
p=subprocess.Popen(['ping','127.0.0.1','-n','10'], shell=False,stdout=False)
相关文档:
import os
import unittest # 包含单元测试模块
import sqlite3 as sqlite # 包含sqlite3模块
def get_db_path():
return "sqlite_testdb"
class TransactionTests(unittest.TestCase): # 单元测试第一步: 由TestCase派生类
def setUp(self): # 单元测试环境配置
......
知识点
1.线程是“轻量级”进程,因为相较于进程的创建和管理,操作系统通常会用较少的资源来创建和管理线程。操作系统要为新建的进程分配单独的内在空间和数据;相反,程序中的线程在相同的内存空间中执行,并共享许多相同的资源。多线程程序在结内存的使用效率要优于多进程程序。
2.python提供了完整的多线 ......
1.获得当前路径
在Python中可以使用os.getcwd()函数获得当前的路径。其原型如下所示。
os.getcwd()
该函数不需要传递参数,它返回当前的目录。需要说明的是,当前目录并不是指脚本所在的目录,而是所运行脚本的目录。例如,在PythonWin中输入如下脚本。
>>> import os
>>> print 'current director ......
PEP 0263
Defining Python Source Code Encodings
Python will default to ASCII as standard encoding if no other
encoding hints are given.
To define a source code encoding, a magic comment must
be placed into the source files either as first or second
line in the file, suc ......
import Queue, threading, sys
from threading import Thread
import time,urllib
# working thread
class Worker(Thread):
worker_count = 0
def __init__( self, workQueue, resultQueue, timeout = 0, **kwds):
Thread.__init__( self, **kwds ) ......