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()

实例三


你可能感兴趣的:(python)