我们在做软件开发的时候很多要用到多线程技术。例如如果做一个下载软件像flashget就要用到、像在线视频工具realplayer也要用到因为要同时下载media stream还要播放。其实例子是很多的。
线程相对进程来说是“轻量级”的,操作系统用较少的资源创建和管理线程。程序中的线程在相同的内存空间中执行,并共享许多相同的资源。
1) 在python中如何创建一个线程对象?
如果你要创建一个线程对象,很简单,只要你的类继承threading.Thread,然后在__init__里首先调用threading.Thread的__init__方法即可:
以前一直使用python的urllib2来抓取页面采集内容,偶然发现python有一个非常好用的封装好的库requests可以省去很多事。具体安装和使用教程可以直接参考官方文档:
简单的写了一个通过requests多线程获取页面数据的代码。不贴所有代码了,代码如下:
Python函数参数可以用星号+形参名形式(*arg或**arg)方式。其中*arg方式必须在传统参数后边出现,而**arg则必须在*arg后出现。
例如:
def test(x, y=5, *a, **b):
print x, y, a, b
函数调用结果:
test(1) ===> 1 5 () {}
test(1,2) ===> 1 2 () {}
test(1,2,3) ===> 1 2 (3,) {}
test(1,2,3,4) ===> 1 2 (3,4)
test(
GD库国旗一面
$ing = imagecreatetruecolor(700,410);
$red = imagecolorallocate($ing,255,0,0);
$yellw=imagecolorallocate($ing,255,255,45);
imagefill($ing,0,0,$red);
$a = array(90,30,108,73,157,73,119,102,135,152,93,123,52,152,66,102,29,74,76,73,90,30);
imagefilledpolygon($ing,$a,10,$yellw);
这个UI是用Qt designer生成的,自己没有系统的学习过Python,有好多东西都走了弯路了!
这里记录一下,感谢google,感谢stackoverflow,感谢csdn
只是有一样不爽,现在google.com.hk搜索出来的东西不能直接打开了,只能挂代理上....
代码:
获得文件扩展名函数:返回扩展名 和 扩名之前的文件名路径。
os.path.splitext('xiaomizhou.jpg')
返回('xiaomizhou' ,'.jpg')
os和os.path模块
os.listdir(dirname):列出dirname下的目录和文件
os.getcwd():获得当前工作目录
os.curdir:返回但前目录('.')
os.chdir(dirname):改变工作目录到dirname
os.path.isdir(name):判断name是不是一个目录,name不是目录就返回false
os.path.isfile(name):判断n
#! /usr/bin/env python
#coding=utf-8
# FIlename ex1.py
import threading
from time import sleep
def test_func(id):
for i in range(0,5):
sleep(1)
print('thread %d is running %d' % (id,i))
threads = []
for i in range(0,3):
t = threading.Thread(target = test_func ,args=(i,))
threads.a
def md5sum(s):
try:
import hashlib
m = hashlib.md5()
m.update(s)
return m.hexdigest()
except:
import md5
m = md5.new()
m.update(s)
return m.hexdigest()
这只是一个练习的DEMO
代码如下:
# -*- coding: utf-8 -*-
from urllib import urlencode
import cookielib, urllib2,urllib
def __login():
headers={'User-Agent':'Mozilla/5.0 (Windows;U;Windows NT 5.1;zh-CN;rv:1.9.2.9)Gecko/20100824 Firefox/3.6.9'}
values = {'form_email':'xxxx@xx.com','form_passwor
1. 新建工程。
打开Eric4,选择菜单Project->New新建一个工程,名字我们取为HelloPyQt,填好各项并选择工程所在文件夹之后点OK,一个新的不含任何文件的工程就建好了。
2. 新建对话框。
在
左侧的ProjectViewer中切换到Forms选项卡(左数第二个),右键点空白位置,选New
Form,在弹出的对话框中选择Form类型为Dialog,然后会问你保存到哪。我设定为保存为DlgHello.ui文件。点OK之后就会新建这个文
件并自动打开QtDesigner。