python 多线程实例

方式一

import requests
import threading

def get_stock(code):
    url = 'http://hq.sinajs.cn/list=' + code
    resp = requests.get(url)
    print('%s\n' % resp.text)

#多线程异步,加速抓取
#根据有几个股票代码,就创建几个线程
codes = ['sz000878', 'sh600993', 'sz000002', 'sz002230']
threads = [threading.Thread(target=get_stock, args=(code, )) for code in codes]
#Thread创建线程实例
'''
threads=[ ]
for code in codes:
    thread=threading.Thread(target=get_stock,args=(code, ))
    threads.append(thread)
'''
for t in threads:
    t.start()  #启动一个线程
for t in threads:
    t.join()  #等待每个线程执行结束

实例二

def getdata():
	# 获取数据
	pass

def having(i,q):
    while True:
        print('======================')
        getdata(q.get())
        if q.empty():
            return 0

if __name__ == '__main__':
    import threading
    conn=psycopg2.connect(database='testdb',user='postgres',password='123456',host='121.52.246.213',port='5432')
    cursor=conn.cursor()  # 创建一个Cursor
    cursor.execute("select fstdomain from fstdomain")
    q = Queue(100) 
    for row in cursor.fetchall():  # 从fetchall中读取操作
        print(row)
        url=row[0]
        print(url)
        q.put(url)
    cursor.close()  # 关闭Cursor
    conn.close()  # 关闭数据库

    for i in range(2):
        # p = multiprocessing.Process(target=having, args=(i,q))
        p = threading.Thread(target=having, args=(i,q))
        p.start()

实例三


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值