is python capable of running on multiple cores?

前端 未结 7 1031
悲&欢浪女
悲&欢浪女 2020-11-28 03:58

Question: Because of python\'s use of \"GIL\" is python capable running its separate threads simultaneously?


Info:

After reading this I came away rathe

7条回答
  •  暖寄归人
    2020-11-28 04:30

    The answer is "Yes, But..."

    But cPython cannot when you are using regular threads for concurrency.

    You can either use something like multiprocessing, celery or mpi4py to split the parallel work into another process;

    Or you can use something like Jython or IronPython to use an alternative interpreter that doesn't have a GIL.

    A softer solution is to use libraries that don't run afoul of the GIL for heavy CPU tasks, for instance numpy can do the heavy lifting while not retaining the GIL, so other python threads can proceed. You can also use the ctypes library in this way.

    If you are not doing CPU bound work, you can ignore the GIL issue entirely (kind of) since python won't aquire the GIL while it's waiting for IO.

提交回复
热议问题