4 ms·
Are your current python programs slow and it matters? Why havent you implemented multithreading? (don't get me wrong, I know the cost of implementation, but i
by hospitalJail 3y ago
Are your current python programs slow and it matters?
Why havent you implemented multithreading?
(don't get me wrong, I know the cost of implementation, but if speed matters, multithreading is a very reasonable step in python)
- birdyrooster 3y agoBecause the GIL and also you have to use tons of locks to get around the lack of thread safety for pythons objects.
- deleted 3y ago[deleted]
- Spivak 3y ago> Why haven't you implemented multi-threading? Because that makes programs slower in Python. Multi-threading in Python is for when you need time-slicing for CPU intensive tasks so that they don't block other work that needs to be be done.
- coldtea 3y agoHis point remains, he just phrased it badly. We haven't you implemented a multiprocess pool?
- gpderetta 3y agoBecause not everything is trivially parallelizable and multiprocess makes it harder to share data?
- coldtea 3y ago>Because not everything is trivially parallelizable A lot of things are though...
- munch117 3y agoBecause it's a global solution to a local problem. With threads, I can encapsulate the use of threads in a class, whose clients never even notice that threads are in use. Sure, threads are a global resource too, but much of the time you can get away with pretending that they're not and create them on demand. Not so with multiprocess. If you use that, then the whole program has to be onboard with it. Threads work great in Python. Well not for maximising multicore performance, of course, but for other things, for structuring programs they're great. Just shuttle work items and results back and forth using queue.Queue, and you're golden - Python threads are super reliable. And if the threads are doing mostly GIL-releasing stuff, then even multicore performance can be good.
- coldtea 3y ago>Not so with multiprocess. If you use that, then the whole program has to be onboard with it Huh? In Python you just need a function to call, and multiprocess will run it wrapped in a process from the pool, while api-wise it would look as it would if it was a threadpool (but with no sharing in the process case, obviously). So what would the rest of the program be onboard with? And all this could also be hidden inside some subpackage within your package, the rest of the program doesn't need to know anything, except to collect the results.
- munch117 3y agomultiprocessing needs to run copies of your program that are sufficiently initialised that they can execute the function, yet no initialisation code must be run that should not be run multiple times. That means you either use fork - which is a major can of worms for a reusable library to use. Or you write something like this in your entry point module: if __name__=='__main__': multiprocessing.freeze_support() once_only_application_code() Suppose I don't realise that your library is using multiprocessing, and I carelessly call it from this two-line script: import library_that_uses_multiprocessing_internally library_that_uses_multiprocessing_internally.do_stuff() That's basically a fork bomb. And where do you put the multiprocessing.set_start_method call? Surely not in the library.