【详解Python中的多线程编程】教程文章相关的互联网学习教程文章

Python多线程编程(三):threading.Thread类的重要函数和方法

这篇文章主要介绍threading模块中的主类Thread的一些主要方法,实例代码如下:代码如下: Created on 2012-9-7 @author: walfred @module: thread.ThreadTest3 @description: import threading class MyThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): print "I am %s" % (self.name) if __name__ == "__main__": for i in range(0, 5): my_thread = MyTh...

Python多线程编程(六):可重入锁RLock

考虑这种情况:如果一个线程遇到锁嵌套的情况该怎么办,这个嵌套是指当我一个线程在获取临界资源时,又需要再次获取。 根据这种情况,代码如下:代码如下: Created on 2012-9-8 @author: walfred @module: thread.ThreadTest6 import threading import time counter = 0 mutex = threading.Lock() class MyThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): glo...

Python多线程编程(一):threading模块综述

Python这门解释性语言也有专门的线程模型,Python虚拟机使用GIL(Global Interpreter Lock,全局解释器锁)来互斥线程对共享资源的访问,但暂时无法利用多处理器的优势。在Python中我们主要是通过thread和 threading这两个模块来实现的,其中Python的threading模块是对thread做了一些包装的,可以更加方便的被使用,所以我们使用 threading模块实现多线程编程。这篇文章我们主要来看看Python对多线程编程的支持。 在语言层面,Pyth...

python多线程用法实例详解

本文实例分析了python多线程用法。分享给大家供大家参考。具体如下: 今天在学习尝试学习python多线程的时候,突然发现自己一直对super的用法不是很清楚,所以先总结一些遇到的问题。当我尝试编写下面的代码的时候:代码如下:class A():def __init__( self ):print "A" class B( A ):def __init__( self ):super( B, self ).__init__( ) # A.__init__( self )print "B" b = B() 出现:代码如下:class A( object ):def __init__( s...

python基于queue和threading实现多线程下载实例

本文实例讲述了python基于queue和threading实现多线程下载的方法,分享给大家供大家参考。具体方法如下: 主代码如下:#download worker queue_download = Queue.Queue(0) DOWNLOAD_WORKERS = 20 for i in range(DOWNLOAD_WORKERS): DownloadWorker(queue_download).start() #start a download worker for md5 in MD5S: queue_download.put(md5) for i in range(DOWNLOAD_WORKERS): queue_download.put(None) 其中downloadworkers....

探寻python多线程ctrl+c退出问题解决方案

场景: 经常会遇到下述问题:很多io busy的应用采取多线程的方式来解决,但这时候会发现python命令行不响应ctrl-c 了,而对应的java代码则没有问题:代码如下: public class Test { public static void main(String[] args) throws Exception { new Thread(new Runnable() { public void run() { long start = System.currentTimeMillis(); while (true) { try { Thread.sleep(1000); } catch (Exception e) { } System....

Python多线程和队列操作实例

Python3,开一个线程,间隔1秒把一个递增的数字写入队列,再开一个线程,从队列中取出数字并打印到终端代码如下: #! /usr/bin/env python3 import time import threading import queue # 一个线程,间隔一定的时间,把一个递增的数字写入队列 # 生产者 class Producer(threading.Thread):def __init__(self, work_queue):super().__init__() # 必须调用self.work_queue = work_queuedef run(self):num = 1while True:self.work_que...

用Python实现一个简单的多线程TCP服务器的教程【图】

最近看《python核心编程》,书中实现了一个简单的1对1的TCPserver,但是在实际使用中1对1的形势明显是不行的,所以研究了一下如何在server端通过启动不同的线程(进程)来实现每个链接一个线程。 其实python在类的设计上已经考虑到了这一方面的需求,我们只要在自己的server上继承一下SocketServer.BaseRequestHandler就可以了。 server端代码如下:#!/usr/bin/env python import SocketServer from time import ctime HOST = ...

浅析Python多线程下的变量问题

在多线程环境下,每个线程都有自己的数据。一个线程使用自己的局部变量比使用全局变量好,因为局部变量只有线程自己能看见,不会影响其他线程,而全局变量的修改必须加锁。 但是局部变量也有问题,就是在函数调用的时候,传递起来很麻烦:def process_student(name):std = Student(name)# std是局部变量,但是每个函数都要用它,因此必须传进去:do_task_1(std)do_task_2(std)def do_task_1(std):do_subtask_1(std)do_subtask_2(st...

Python实现多线程抓取妹子图

心血来潮写了个多线程抓妹子图,虽然代码还是有一些瑕疵,但是还是记录下来,分享给大家。 Pic_downloader.py# -*- coding: utf-8 -*- """ Created on Fri Aug 07 17:30:58 2015@author: Dreace """ import urllib2 import sys import time import os import random from multiprocessing.dummy import Pool as ThreadPool type_ = sys.getfilesystemencoding() def rename():return time.strftime("%Y%m%d%H%M%S") def rename_2(...

尝试使用Python多线程抓取代理服务器IP地址的示例

这里以抓取 http://www.proxy.com.ru 站点的代理服务器为例,代码如下:#!/usr/bin/env python #coding:utf-8 import urllib2 import re import threading import time import MySQLdb rawProxyList = [] checkedProxyList = [] #抓取代理网站 targets = [] for i in xrange(1,42):target = r"http://www.proxy.com.ru/list_%d.html" % itargets.append(target) #抓取代理服务器正则 p = re.compile(r(\d+)(.+?)(\d+)(.+?)(.+?)) #...

Python多线程下载文件的方法

本文实例讲述了Python多线程下载文件的方法。分享给大家供大家参考。具体实现方法如下:import httplib import urllib2 import time from threading import Thread from Queue import Queue from time import sleep proxy = your proxy; opener = urllib2.build_opener( urllib2.ProxyHandler({http:proxy}) ) urllib2.install_opener( opener ) ids = {}; for i in range(1,110):try:listUrl = "http://www.someweb.net/sort/list...

学以致用,python多线程备份数据库并删除旧的备份。

#!/usr/bin/python2 # -*- coding=utf-8 -*-3 import time4 import os5 import datetime6 import threading7 from time import ctime,sleep8 9 date=time.strftime(‘%Y-%m-%d‘,time.localtime(time.time())) 10 dbname=(‘test‘,‘test2‘) #定义元组必须要有多个,要不则循环里面的字符。 11 dbname2=(‘test3‘,‘test4‘) 12 bkdir="/backup/mysqlbk/" 13 14 #删除超过3天的备份文件 15 now_time = datetime.datetime.now()...

第十一节:python mysql交互、socket、多线程【代码】

python个人笔记,纯属方便查询。------------------------------------python mysql交互--------------------------------------- #查询: import MySQLdb try: conn=MySQLdb.connect(host=‘10.86.10.21‘,user=‘root‘,passwd=‘mysql‘,db=‘python‘,port=3306) cur=conn.cursor() cur.execute(‘select * from test111‘) print cur.fetchall() #取全部行 print cur.fetchmany(...

python多线程应用——DB2数据库备份【代码】

前言:DB2一个实例下,可以存在多个数据库,之前使用shell备份脚本,但是同一时刻只能备份一个数据库,对于几百G的备份文件,这个速度显然太慢,今天学习了Python多线程,刚好应用一下。 分析:1、磁盘I/O允许情况下,使用多线程,节省时间,相当可行。2、Python多线程在某些场景上是鸡肋,但是对于I/O密集型的场景最为适用,这里刚好。3、thread模块有诸多问题,这里使用threading模块。4、先前备份脚本修改端口来清理已连接应用...