1

the csv:

email,password
[email protected],SuperSecretpassword123!
[email protected],SuperSecretpassword123!
[email protected],SuperSecretpassword123!
[email protected],SuperSecretpassword123!

the example printing function

def start_printer(row):
    email = row["email"]
    password = row["password"]

    print(f"{email}:{password}")

the threading starting example

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

how do I pass the values from the csv to the threading example?

7
  • What are you having trouble with? Opening the csv file? Passing it to csv.reader? Looping over the rows? Commented May 25, 2022 at 2:49
  • i have problems initializing the whole logic ... Commented May 25, 2022 at 2:54
  • like passing the arguments from the csv to the threading ... Commented May 25, 2022 at 2:56
  • What have you tried? If you have trouble opening a file for example, write a question specifically about that - currently your question comes down to "please write this for me" and that's not what SO is for. If you have solved part of the problem, but just have a problem with this part, please share the code you have so far to solve the problem. Commented May 25, 2022 at 3:06
  • I think the whole problem is my understanding with threading it has nothing to do with like "can you do it for me" I really don't understand how to pass the arguments to the threading Commented May 25, 2022 at 3:08

1 Answer 1

1

I guess you could go about it this way:

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

Result:

[email protected]:SuperSecretpassword123!
[email protected]:SuperSecretpassword123!
[email protected]:SuperSecretpassword123!
[email protected]:SuperSecretpassword123!
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.