Experimenting with threading and multiprocessing modules, python
This question already has an answer here:
Both of your questions can be answered by this excerpt from the docs:
"The mechanism used by the CPython interpreter to assure that only one thread executes Python bytecode at a time. This simplifies the CPython implementation by making the object model (including critical built-in types such as dict) implicitly safe against concurrent access. Locking the entire interpreter makes it easier for the interpreter to be multi-threaded, at the expense of much of the parallelism afforded by multi-processor machines."
Bold emphasis mine. You end up spending a lot of time switching between threads to make sure they all run to completion. It's like cutting a rope into small pieces and tying the pieces back together again, one string at a time.
Your results with multiprocessing module are as expected since each process executes in its own address space. These processes are independent of one another, besides being in the same process group and having the same parent process.
链接地址: http://www.djcxy.com/p/55198.html上一篇: 使用asyncio进行简单的异步任务
下一篇: 试验线程和多处理模块,python