1

I am trying to use two threads in a server program, one for listening for any communications from clients using the Twisted library, and the other for doing some other computations on the server. In my attempt to implement the threads, it seems that the python threading library doesn't support parallel threads as answered in this question. I was wondering if there is any other python library that addresses this problem? Or any other way to circumvent this limitation? Thank you in advance.

Community
  • 1
  • 1
user823743
  • 2,152
  • 3
  • 21
  • 31
  • 1
    The [`multiprocessing`](https://docs.python.org/2/library/multiprocessing.html) module uses processes for concurrency instead of threads, so it's not affected by the GIL. – dano Oct 30 '14 at 14:19

1 Answers1

2

Python's GIL (global interpreter lock) prevents two threads to simultaneously execute Python code. Fortunately that doesn't include I/O, so if your threads do significant amounts of networking, database, or filesystem, then usual threads do work correctly. They won't let you take advantage of multiple cores for computation, but will let other threads advance while any number of them are waiting for something to happen.

If your needs are more for computation than for I/O, then threads (as implemented on Python) won't help. Better use the multiprocessing module (standard since Python 2.6), which uses a 'thread-like' API to spawn multiple processes, each one with an independent Python interpreter, and therefore it's own GIL.

Javier
  • 60,510
  • 8
  • 78
  • 126