python的并行开发有两种方式:fork和thread(线程)。thread比fork更轻量级,具有更好运行效率和可移植性,在需要进行并行操作的场合首推thread。 python标准库内置一个thread模块,该模块提供一个轻便简易的多线程编程接口,可以无需任何修改就能够运行在Win、Solaris、Linux等操作系统上。浏览一下thread模块: import thread dir(thread) 看到 ['LockType', '__doc__', '__name__', '_local', 'allocate', 'allocate_lock', 'e...
by tessykandy - Python文档中心 - 2009-03-23 13:48:49 阅读(1601) 回复(0)
看下这段代码,pygame写的一个循环播放背景音乐:[code]#-*- coding: utf-8 -*- #file:test.py # import threading import pygame pygame.mixer.init() class player(threading.Thread): def __init__(self,song_path,end_flag): threading.Thread.__init__(self) self.end_flag = end_flag self.song_path = song_path def run(self): pygame.mixer.music.load(self.song_path) ...
下面的代码,运行一段时间就报错,无法创建新的线程,还请高手指点一下。[code]import socket ,threading ip=('192.168.1.20') def abc(ip,post): sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sk.settimeout(1) try: sk.connect((ip,post)) print 'Server port %d OK!'%(post) except Exception: pass #print 'Server port %d not connect!'%(post) sk.close(...
python 多线程读取数据库 [code] from mysql import MYSQL import threading zzzzz = [] def run(z,zz): SQL = "select * from e_verify_email where Id = \'"+zz+"\'" my = MYSQL(SQL) alldata = my.getRs() zzzzz.append(alldata) if __name__ == "__main__": threads = [] k = 5 for a in range(0,k): z = threading.Thread(target=run,args=(a,str(a+1...
[code] import threading,urllib def openurl(): while 1: url = urllib.urlopen('http://domain.com') if __name__=='__main__': for i in range(100): i = threading.Thread(target = openurl) i.start() [/code] 写着玩的.
一个简单的测试,两个线程,在线程中打印日志,日志打印到文件中,发现多线程无法打印日志。 thread1.py代码如下:[code]import logger.log import threading import t log = logger.log.Logger.getLogger(__name__) def thread1(): t.test('1') def thread2(): t.test('2') if __name__ == '__main__': log.debug('start main') t1 = threading.Timer(10.0, thread1) t1.start() t2 = threading.Tim...
本帖最后由 SeriousCool 于 2013-12-08 18:35 编辑 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 ''' import sys import os import time import urllib from threading import Thread local_proxies = {'http': 'http:/...
本帖最后由 tank064 于 2013-11-20 18:04 编辑 hello 最近在倒腾一个可以post提交用户名密码的下载脚本, 在网上搜索了下,找到不少相关文章. 下面是一段代码[code]class Axelpython(Thread, urllib.FancyURLopener): '''Multi-thread downloading class. run() is a vitural method of Thread. ''' def __init__(self, threadname, url, filename, ranges=0, proxies={}): Thread.__init__(self, ...