1. 程式人生 > 其它 >Pandas:從CSV中讀取一個含有datetime型別的DataFrame

Pandas:從CSV中讀取一個含有datetime型別的DataFrame

1、執行緒:

程式的最小的執行單位
py檔案就是一個程序,這個程序裡面本來就是有一個執行緒的,本來的這個執行緒,就叫做主執行緒
執行緒和執行緒之間是獨立的。

建立、啟動執行緒
t = threading.Thread(target=create_data,args=(i,)) #建立執行緒
t.start() #啟動執行緒、
threading.activeCount() #當前執行緒數
主執行緒等待子執行緒的兩種方式
import threading
import time,random
def create_data(url):
    t = random.randint(1,5)
    print(
"t..",t) time.sleep(t) print("url",url) start_time = time.time() #第一種主執行緒等待子執行緒的方式 # threads = [] # for i in range(5): # t = threading.Thread(target=create_data,args=(i,)) # t.start() # threads.append(t) # # for t in threads: # t.join() #第二種 通過判斷當前執行緒數的方式 for i in range(5): t
= threading.Thread(target=create_data,args=(i,)) t.start() while threading.activeCount() !=1: pass end_time = time.time() print('執行時間',end_time - start_time) print(threading.activeCount()) #當前的執行緒數

2、多執行緒-- 鎖

·  多個執行緒同時操作同一個資料的時候

lock = threading.Lock()
lock.acquire() #建立鎖
lock.release() #釋放鎖
建立鎖後需要釋放鎖,不然會成為死鎖
import threading
count = 0

lock = threading.Lock()

def add_data():
    print("當前的執行緒是",threading.currentThread())
    global count
    lock.acquire() #建立鎖
    count+=1
    lock.release()#釋放鎖
    with lock:
        count+=1


for i in range(10):
    t = threading.Thread(target=add_data )
    t.start()

while threading.activeCount() != 1:
    pass

print(count)

3、守護執行緒:

主執行緒結束,子執行緒也全部結束
t.setDaemon(True) #把子執行緒設定成守護執行緒
import threading
import time
def ganhuo():
    time.sleep(10)
    print("done!")

for i in  range(10):
    t = threading.Thread(target=ganhuo)
    t.setDaemon(True) #把子執行緒設定成守護執行緒
    t.start()

print("主執行緒活幹完了")
4、執行緒池:threadpool 模組
pool = threadpool.ThreadPool(10) #建立執行緒池,引數是執行緒數
import requests,threading
import os,time,random
import threadpool

image_url = "https://q4.qlogo.cn/g?b=qq&nk=%s&s=140"

# qq_numbers = ["511402865","260303838","1099276647","1490768397","729226162",
#       "1164019076","569380213","923296994","330547222","609924484","1054114694",
#       "854795345"]
qq_numbers = [
    ["511402865","http://511402865"],
    ["511402861","http://511402865"],
    ["511402861","http://511402865"],
    ["511402861","http://511402865"],
    ["511402861","http://511402865"],
    ["511402861","http://511402865"],
    ["511402861","http://511402865"],
    ["511402861","http://511402865"],
]
def down_load_pic(arg_list): #qq,url,
    qq = arg_list[0]
    url = arg_list[1]
    print(qq,url)
    # url = image_url % qq
    # req = requests.get(url)
    # path = os.path.join("images",qq+'.jpg')
    # with open(path,'wb') as fw:
    #     fw.write(req.content)

pool = threadpool.ThreadPool(10)

reqs = threadpool.makeRequests(down_load_pic,qq_numbers)#分配資料
for req in reqs: #把分配好的資料,放到執行緒池裡面
    pool.putRequest(req)

pool.wait() #開始執行

5、繼承的方式啟動執行緒:

import threading,time

class MyThread(threading.Thread):
    def __init__(self,name):
        super().__init__()
        self.name = name


    def run(self):
        time.sleep(1)
        # print("%s建立資料"%self.name)
        print("建立資料")

for i in range(5):
    t = MyThread("name")
    t.start()