2

I am looking for an explanation by someone who has worked in depth with both languages. Someone who has worked with C# and C++, I know C# provides a task parallel library from .Net 4.0 onwards. C++ also has a concurrent runtime library http://msdn.microsoft.com/en-us/library/dd504870.aspx.

How can one achieve concurrency in python webapps and not spawn processes but rather threads for tasks. Assuming the user is able to manage locks etc.

Syler
  • 1,151
  • 1
  • 13
  • 30

2 Answers2

2

Warning: concurrency is not parallelism. Warren is correct that the Global Interpreter Lock (GIL) limits parallelism within a single Python process, but if the process is IO-bound a select-based application can provide sufficient concurrency without parallelism (Python's asyncore documentation has a good discussion of the select-based workflow).

That said, Python's Twisted framework provides future-like objects called deferreds and does multi-threading under the hood (obviously the GIL limits each process to a single thread at any given instant). Though some find Twisted too low level and/or deferreds to complicated for webapp development.

In the extreme it is possible for native code called from Python to release the GIL if the native code is not going to interact with Python objects. For example ctypes provides a rudimentary interface to C functions which releases the GIL while the C function is running. Similar results can be achieved with other extension frameworks like Boost.Python and SIP.

Community
  • 1
  • 1
Stephen
  • 93
  • 1
  • 5
  • +1 for "concurrency is not parallelism". As a side note, `asyncore` is deprecated since Python 3.6, [asyncio](https://docs.python.org/3/library/asyncio.html) should be used instead. – wombatonfire Aug 23 '20 at 13:39
1

Python has a global interpreter lock that prevents more than one thread from executing at a time, essentially preventing concurrency. That's why you see people spawning processes when they want concurrency in Python. C# and C++ in contrast permit concurrent threads.

Additional information on why Python has a global interpreter lock is available at Why the Global Interpreter Lock?.

Community
  • 1
  • 1
Warren Dew
  • 8,790
  • 3
  • 30
  • 44