3月 15

python 在 crontab 中的调用

python 在 crontab 中的调用

  使用python写了一个脚本,手动执行没有问题。需要自动运行,所以要配置到crontab中。举例脚本位置“/Data/script/test.py” ,其中包含读取操作系统、读取配置文件、写日志操作,使用root用户每5分钟执行一次。最初配置为:

*/5 * * * * /usr/local/bin/python /Data/script/test.py >/dev/null

  需要注意的是“*/5 * * * *”中间要用空格分隔。“/usr/local/bin/python”要根据使用的python脚本安装位置填写,通过观察定时任务失败。
  分析原因,crontab运行时的环境变量与ssh登录的环境变量不同,导致读取配置目录和文件失败。解决方法,写一个shell调用脚本,将脚本放在crontab中。脚本有一个参数“start”。
shell脚本为test.sh:

#! /bin/bash
#
# crontab shell python
#
# www.simonzhang.net
# email:simon-zzm@163.com
#
### END INIT INFO

. /etc/profile
cd /Data/script/

case "$1" in
  start)
      /usr/local/bin/python /Data/script/test.py start &
     ;;
  test)
      /usr/local/bin/python /Data/script/test.py test &
      ;;
  *)
        echo $"Usage: $0 {start|test}"
        exit 1
esac

exit 1

将脚本配置到crontab中运行成功,配置为:

*/5 * * * * /bin/sh /Data/script/test.sh start >/dev/null
2月 13

python 字典的保存于读取

用字典做了文本数据的统计,但是还没有统计完,需要第二天继续,所以
用到了数据持久化部分。简单记录字典的保存和导出。

import pickle
# create dict
list = {“01″:02,”02″:03,”03”:04}
# save dict
f1 = open(“E:\\test.txt”,”wb”)
pickle.dump(list, f1)
f1.close()
# load dict
f2 = open(“E:\\test.txt”,”rb”)
load_list = pickle.load(f2)
f2.close()
# print
print load_list

理论上也可以放入数据库中,但是我没有测试。

12月 16

python 在linux上处理图片的错误处理

在CentOS 5.4 64位上安装了python2.6和PIL模块处理图片。
遇到问题1:
Traceback (most recent call last):
 File “aimg.py”, line 3, in
  img.save(‘/program/upload/b.jpg’,’JPEG’)
 File “/usr/local/lib/python2.6/site-packages/PIL-1.1.7-py2.6-linux-

x86_64.egg/Image.py”, line 1406, in save
 self.load()
 File “/usr/local/lib/python2.6/site-packages/PIL-1.1.7-py2.6-linux-

x86_64.egg/ImageFile.py”, line 189, in load
  d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
 File “/usr/local/lib/python2.6/site-packages/PIL-1.1.7-py2.6-linux-

x86_64.egg/Image.py”, line 385, in _getdecoder
  raise IOError(“decoder %s not available” % decoder_name)
IOError: decoder jpeg not available

处理方法:
# rm -rf /usr/local/lib/python2.6/site-packages/PIL-1.1.7-py2.6-linux-x86_64.egg
# rm -rf /usr/local/lib/python2.6/site-packages/PIL.pth
# yum install -y libjpeg* freetype* zlib*
# easy_install PIL

遇到问题2:
WARNING: ” not a valid package name; please use only.-separated package names in setup.py
_imaging.c:75:20: error: Python.h: No such file or directory
In file included from libImaging/Imaging.h:14,
from _imaging.c:77:
libImaging/ImPlatform.h:14:2: error: #error Sorry, this library requires support for ANSI prototypes.
libImaging/ImPlatform.h:17:2: error: #error Sorry, this library requires ANSI header files.
libImaging/ImPlatform.h:55:2: error: #error Cannot find required 32-bit integer type
In file included from _imaging.c:77

处理方法:
yum install python-imaging
yum install python-dev
python PIL

再次测试问题解决。

10月 11

突发想法:python 随机产生双色球投注

【2011-10-11 张子萌】
今天用python做redis的测试,主要测试随机读redis的效率。突发想法这个产生双色球的结果挺方便的。33选6 ,呵呵呵呵呵。

#!/bin/env python
# -*- coding:utf-8 -*-
import random
list = [“01″,”02″,”03″,”04″,”05″,”06″,”07″,”08″,”09″,”10″,”11”,
“11”,”12″,”13″,”14″,”15″,”16″,”17″,”18″,”19″,”20″,”21″,
“21”,”22″,”23″,”24″,”25″,”26″,”27″,”28″,”29″,”30″,”31″,
“32”,”33″]
get_array = random.sample(list,6)
print “%s”%get_array

10月 04

python 链接 redis 测试

[2011-9-11 张子萌]
[2012-5-27 张子萌 修改]
  安装支持的模块,简单的方法是使用easy_install
# easy_install redis

  也可以下载安装包手动安装。做个简单的写入测试,使用有664039行的密码表,将密码放到redis中。代码如下:

#!/usr/bin/env python
import redis
def main():
    r = redis.Redis(host='192.168.1.200', port=6379, db=0)
    w_pass=open('big_pass.txt','r').readlines()
    next_line=0
    count_lines=len(w_pass)
    while next_line+1 < count_lines:
        r.set(str(next_line),w_pass[next_line])
        next_line=next_line+1
    print 'Game over!'
if __name__=='__main__':
    main()

测试服务器为一个Xeon(TM)双核 3.20GHz 的 cpu,内存4G。
a)写入
用两台机器做测试。两台机器均在一个千兆局域网内。实验很简单所以只记录了cpu的负载和脚本运行时间。
# time python test_redis1.py
Game over!
real 4m16.878s
user 0m55.910s
sys 0m16.879s

cpu部分
远端机缓存 load average: 1.46, 1.26, 1.34

Redis服务器 load average: 0.52, 0.30, 0.22

单台服务器本地测试:
# time python python_redis.py
Game over!
real 1m32.065s
user 1m2.643s
sys 0m17.890s

cpu部分
load average: 1.32, 0.57, 0.32

b)存环从reids中读取所有数据,测试结果如下:
两台服务器:
# time python test_redis1.py
Game over!

real 4m32.776s
user 1m3.687s
sys 0m17.675s

Redis服务器Cpu部分
load average: 0.90, 0.96, 0.69
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
27525 root 15 0 117m 110m 712 S 7.3 2.8 2:55.36 redis-server

单台服务器本地测试:
# time python python_redis.py
Game over!

real 1m44.790s
user 1m9.294s
sys 0m19.031s

Cpu部分
load average: 1.36, 0.92, 0.63

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3255 root 18 0 153m 47m 2640 R 87.1 1.2 0:29.43 python
27525 root 15 0 117m 110m 712 S 33.1 2.8 2:29.26 redis-server

Redis的其它操作并未测试,但是从当前测试看,单进程每秒读写差不多再7000-8000,cpu使用维持在百分之三十多,确实效率不错。
注为了测试方便,我的redis服务没有加密码。如需加访问密码或有其它需参数,参考如下:
host='localhost'
port=6379
db=0
password=None
socket_timeout=None
connection_pool=None
charset='utf-8'
errors='strict'
unix_socket_path=None