Python - multithreading + csv

Asked

Viewed 101 times

0

I’m trying to write csv files using a Thread for each file. Code iterates a list generated by groupby and calls a thread for each group.

Calling the function directly files are saved normally:

[save_csv(Uf[0]+'.csv', header, Uf[1]) for Uf in ufs]

Using Threads, files are saved only with the header, even if the function is run to the end

[threading. Thread(target=(save_csv), args=(Uf[0]+'.csv', header, Uf[1]). start() for Uf in ufs]

Function save_csv:

def save_csv(file, header, content):
    with open(file, 'w', newline='', encoding='utf-8') as f:
        writer = csv.DictWriter(f, delimiter=';', fieldnames=header)
        writer.writeheader()
        writer.writerows(list(map(vars, content)))

Is there a reason for this to happen?

Note: The code is using object orientation and threads as required by the exercise list.

1 answer

0


the variables "vars" and "content" are global so they will be accessed in parallel in all threads. You don’t talk and you don’t show how they’re created, you can’t know and you can’t give the right answer.

It is only possible to say that since you do not pass as parameter, all threads " will see the same content of the variable "Contents". If you use "multiprocessing", yes, a "photo" of global data structures is taken at the moment a subprocess is created, and takes life independently. With threads, all threads see the same variable.

  • Clarified the doubt, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.