Python 专题三 python 多线程

一.Join操作

import threading
import time
class MyThread(threading.Thread):
    def __init__(self,id):
        threading.Thread.__init__(self)
        self.id=id
    def run(self):
        time.sleep(10)
        print self.id
def func():
    for i in range(5):
        print i
t=MyThread(2)
t.start()
t.join()
func()
1.如果没有t.join,会先把func执行完,直接输出0,1,2,3,4,5,等待10秒后输出2

2.如果有t.join,先等待10秒输出2,再执行func

二.Gevent

import gevent
from gevent import monkey
monkey.patch_all()

import requests
import re

url='http://www.vpsgo.com/top-100/'
htmltext=requests.get(url).content
pattern=re.compile(r"(http://www.vpsgo.com/movies/.*?\")")
links=re.findall(pattern,htmltext)

def worker(link,num):
    with requests.Session() as s:
        s.get(link)
    print 'requesting url number',num
    print 'done'
    print '=' * 25
for num,link in enumerate(links):
    jobs=[]
    jobs.append(gevent.spawn(worker,link,num))
gevent.joinall(jobs)
gevent.shutdown()

三.爬虫

from threading import Thread
import urllib
import re

def th(ur):
    base ="http://finance.yahoo.com/q?s="+ur
    regex='<span id="yfs_184_'+ur.lower()+'">(.+?)</span>'
    pattern=re.compile(regex)
    htmltext=urllib.urlopen(base).read()
    results=re.findall(pattern,htmltext)
    print "the price of "+str(ur)+"is"+str(results)

#symbolslist=open("symbols.txt").read()
#symbolslist=symbolslist.replace(" ","").split(",")

#print symbolslist
symbolslist=["BIOC","ACCL"]

threadlist=[]

for u in symbolslist:
    t=Thread(target=th,args=(u,))
    t.start()
    threadlist.append(t)

for b in threadlist:
    b.join()





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值