Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - should I use threads or processes for network activity?

I am writing a python script that will upload a file to a remote FTP server when the enter key is pressed on the keyboard.

Since the enter key can be pressed randomly, multiple threads/processes could spawn concurrently.

Now should I use threading or multiprocessing to execute the upload? Which is better and why?

like image 230
Debanik Dawn Avatar asked Dec 21 '25 14:12

Debanik Dawn


1 Answers

So for real parallel work you would need multiprocessing, since threads only gives an advantage in a few cases (like IO).

As already pointed out, for your IO work threads should be fine, although you should look into multiprocessing, since from my experience it isn't hard to implement either.

Also it's way easier to get a return value from multiprocessing than from threads

If you want to know why threads is slower for most use cases you should read this: https://jeffknupp.com/blog/2012/03/31/pythons-hardest-problem/

Edit: Also keep in mind that for simple tasks threads and multiprocessing actually increase the runtime, since the overhead from allocating threads/processes and higher memory usage from multiprocessing due to no shared memory are quite significant.

like image 140
Lucy The Brazen Avatar answered Dec 23 '25 03:12

Lucy The Brazen