因为工作需要,从Python 2.5开始学习Python,当然只是简单的使用Python完成一些工作,没有很少深入学习。一次,在一个多CPU的环境中,想用Python的多线程编程来达到使用完系统的CPU的目的,结果是不行的。最后发现,Python 2.6以后提供了一个multiprocessing的库,可以达到这个目的。
分享如下:
##多线程编程
[……]
本节将展示一种代码模板,它会以‘手动’方式向JWSDP的webservice(document/literal)传递SOAP消息。后面也会用相同的方法访问 Infobel webservice。
1.XML通过HTTP请求到JWSDP webservice
使用python向web服务器传送一个文件相对容易些。对于JWSDP webservice,不要忘了在消息头中增加Content-type,只有这样,调用才能被服务器正常接收。下面是一个命令行示例,SOAP响应都不加解析地被打印出来。由于返回数据相对复杂,我会在稍后的Infobel web service示例中讲解如何使用PyXml解析SOAP消息。
例3.如何用python传递SOAP消息
在接下来的示例中我们访问readLS(),它带有一个string类型的返回值。
[……]
以下程序为三个数字的排序,并计算所用时间。
#!/usr/bin/env python
def test1(x1=8,x2=5,x3=10,order=None):
”’ default is ascending order ”’
temp1=x1
temp2=x2
temp3=x3
if temp1 > temp2:
temp1, temp2 = temp2, temp1
if temp2 > temp3:
temp2, temp3 = temp3, temp2
if temp1 > temp2:
temp1, temp2 = temp2, temp1
if order is None:
return [temp1, temp2, temp3]
else:
return [temp3, temp2, temp1]
[……]
这个程序我还没有进行过验证,不知道是否可用。放到这里待以后方便学习吧。
Python语言: Python实现多线程下载
#!/usr/bin/python
# -*- coding: utf-8 -*-
# filename: paxel.py
”’It is a multi-thread downloading tool
It was developed follow axel.
Author: volans
E-mail: volansw [at] gmail.com
”'[……]
#coding=utf-8
from functools import partial
class DES(object):
“””
DES加密算法
interface: input_key(s, base=10), encode(s), decode(s)
“””
__ip = [
58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,64,56,48,40,32,24,16,8,
57,49,41,33,25,17, 9,1,59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,63,55,47,39,31,23,15,7,
]
[……]
Evolution of a Python programmer
#Newbie programmer
def factorial(x):
if x == 0:
return 1
else:
return x * factorial(x – 1)
print factorial(6)
[……]
最近遇到一个问题,就是在双网卡的Linux主机中,需要增加多个IP地址,有的时候还需要增加一个网段。
先把几个增加的方法介绍如下:
1. 在网卡eth0上增加一个IP地址192.168.1.22
[……]