Python 将 .csv 文件中的参数传递给线程
Python pass arguments from a .csv file to threads
csv:
email,password
test0@test.com,SuperSecretpassword123!
test1@test.com,SuperSecretpassword123!
test2@test.com,SuperSecretpassword123!
test3@test.com,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 传递到线程示例?
我想你可以这样做:
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()
结果:
test0@test.com:SuperSecretpassword123!
test1@test.com:SuperSecretpassword123!
test2@test.com:SuperSecretpassword123!
test3@test.com:SuperSecretpassword123!
csv:
email,password
test0@test.com,SuperSecretpassword123!
test1@test.com,SuperSecretpassword123!
test2@test.com,SuperSecretpassword123!
test3@test.com,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 传递到线程示例?
我想你可以这样做:
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()
结果:
test0@test.com:SuperSecretpassword123!
test1@test.com:SuperSecretpassword123!
test2@test.com:SuperSecretpassword123!
test3@test.com:SuperSecretpassword123!