CSV:
email,password
[email protected],SuperSecretpassword123!
[email protected],SuperSecretpassword123!
[email protected],SuperSecretpassword123!
[email protected],SuperSecretpassword123!
示例列印功能
def start_printer(row):
email = row["email"]
password = row["password"]
print(f"{email}:{password}")
執行緒啟動示例
number_of_threads = 10
for _ in range(number_of_threads):
t = Thread(target=start_printer, args=(row,))
time.sleep(0.1)
t.start()
threads.append(t)
for t in threads:
t.join()
如何將值從 csv 傳遞到執行緒示例?
uj5u.com熱心網友回復:
我想你可以這樣做:
from threading import Thread
from csv import DictReader
def returnPartionedList(inputlist: list, x: int = 100) -> list: # returns inputlist split into x parts, default is 100
return([inputlist[i:i x] for i in range(0, len(inputlist), x)])
def start_printer(row) -> None:
email: str = row["email"]
password: str = row["password"]
print(f"{email}:{password}")
def main() -> None:
path: str = r"tasks.csv"
list_tasks: list = []
with open(path) as csv_file:
csv_reader: DictReader = DictReader(csv_file, delimiter=',')
for row in csv_reader:
list_tasks.append(row)
list_tasks_partitions: list = returnPartionedList(list_tasks, 10) # Run 10 threads per partition
for partition in list_tasks_partitions:
threads: list = [Thread(target=start_printer, args=(row,)) for row in partition]
for t in threads:
t.start()
t.join()
if __name__ == "__main__":
main()
結果:
[email protected]:SuperSecretpassword123!
[email protected]:SuperSecretpassword123!
[email protected]:SuperSecretpassword123!
[email protected]:SuperSecretpassword123!
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/481897.html
上一篇:Glib:呼叫迭代回圈函式
