方式一
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]
'''
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.execute("select fstdomain from fstdomain")
q = Queue(100)
for row in cursor.fetchall():
print(row)
url=row[0]
print(url)
q.put(url)
cursor.close()
conn.close()
for i in range(2):
p = threading.Thread(target=having, args=(i,q))
p.start()
实例三