[转自]http://blog.sciencenet.cn/blog-600900-499638.html 最近,我们老大要我写一个守护者程序,对服务器进程进行守护.如果服务器不幸挂掉了,守护者能即时的重启应用程序.上网Google了一下,发现Python有很几个模块都可以创建进程.最终我选择使用subprocess模块,因为在Python手册中有这样一段话: This module intends to replace several other, older modules and func…
Linux 操作系统提供了一个 fork() 函数用来创建子进程,这个函数很特殊,调用一次,返回两次,因为操作系统是将当前的进程(父进程)复制了一份(子进程),然后分别在父进程和子进程内返回.子进程永远返回0,而父进程返回子进程的 PID.我们可以通过判断返回值是不是 0 来判断当前是在父进程还是子进程中执行. 在 Python 中同样提供了 fork() 函数,此函数位于 os 模块下. 下面是一个例子 import os import time print "Before fork proc…
from multiprocessing import Process import os def run(name): print 'The child process '%s' (pid %d) is running' % (name, os.getpid()) return print 'The parent process(pid %d) is running' % os.getpid() p = Process(target=run, args = ('test', )) print…
在类创建的对象中,一般都是以字典的方式来保存信息 class Student: def __init__(self, name, age, score): self.name = name self.age = age self.score = score def input_student_info(): l = [] while True: n = input("请输入学生姓名:") if n == "": break a = input("请输入学生…
from multiprocessing import Process import time import os # # def acb(n): # print(n) # # # if __name__ == '__main__': # p1 = Process(target=acb, args=("ready", )) # p1.start() # time.sleep(1) # print("end") # class MP(Process): # def _…
 python的subprocess模块,看到官方声明里说要尽力避免使用shell=True这个参数,于是测试了一下: from subprocess import call import shlex cmd = "cat test.txt; rm test.txt" call(cmd, shell=True) 运行之后: 1:打开并浏览了test.txt文件 2:删除了test.txt文件 from subprocess import call import shlex cmd =…
我们在自动化测试中经常会需要关联用例处理,需要动态类属性: 推荐使用第二种方法: 创建:setattr() 获取:getattr() 两种,如何创建 类属性 loan_id # 第一种,创建 # 类名.属性名 = 具体的属性值 Context.loan_id = mysql_data.get('Id') # 动态创建类属性 # 第二种,创建 setattr(译:赛特attr) # 如果第一个参数为实例对象, 那么将会为这个实例对象, 创建实例属性 # 如果第一个参数为类, 那么将会创建类型属性…
[code] import numpy as np threshold=2 a=np.array([[1,2,3],[3,4,5]]) b=a>threshold print("a="+str(a)) print("b="+str(b)) [result] a=[[1 2 3] [3 4 5]] b=[[False False True] [ True True True]]…
# 单例模式(使用装饰器) def singleton(cls): instance = {} def wrapper(*args,**kwargs): if cls not in instance: instance[cls] = cls(*args,**kwargs) return instance[cls] return wrapper @singleton class Foo(object): pass foo1 = Foo() foo2 = Foo() print(foo1 is fo…
python中数字对象的创建如下, a = 123 b = 1.23 c = 1+1j 可以直接输入数字,然后赋值给变量. 同样也可是使用类的方式: a = int(123) b = float(1.23) c = complex(1+1j) 但一般不用类的方式创建,直接输入数字就好了. python中的数字包括了整型 int ,长整型 long , 浮点型 float , 复数 complex ,其差别为: int(整型) 也称有符号整数,只有正或负整数,不带小数点. 其和长整型在于整数有一定…