6

I come from python programming background. In python the interpreter runs in a process. Multi threading is recommended for IO operations (file io, http request, etc) where as multi processing is recommended for CPU intensive operations (calculations).

Multi threading means within the processes multiple threads are created. Multi processing means a new process (interpreter) is created for each task.

I understand that C# also has the concept of async/await, Task.Run and Task Parallel library. It appears that these all multi threading (that is - threads running in the same process). So C# does not have multi processing? What is the concept/approach for IO vs CPU bound work in C#?

4
  • Tasks are great for I/O, but they are not threads. Though they do borrow a thread for CPU processing, waiting for I/O does not use one. blog.stephencleary.com/2013/11/there-is-no-thread.html Commented Jul 7, 2022 at 4:51
  • I agree that in the case of async/away Task there are no threads, however in case of Task.Run a new thread is created. Effectively does this mean C# always has 1 process. Commented Jul 7, 2022 at 4:54
  • 1
    Python (actually Cython) will not actually do multiprocessing due to the GIL. async is a method for coroutines which is as stated above not threads. For threads in C# see learn.microsoft.com/en-us/dotnet/api/… Commented Jul 7, 2022 at 4:54
  • 1
    Of course, you can also start a new process in C# Commented Jul 7, 2022 at 5:39

1 Answer 1

4

Both languages are Turing complete, so in principle whatever can be done in one can also be done in the other!

A less trite answer is that, no, typically one does not resort to multiprocessing in C#. Multiprocessing in Python was added because of the global interpretter lock; instead of ridding the interpretter of that lock and facilitating proper multi-threading within a single Python process, it was easier (at the time - Python was a much smaller community at that time, fewer volunteers, etc; important considerations) to implement multiprocessing and get something close to the same sort of result with less work required. The end result is, so far as I'm concerned, actually quite good, being a lot closer to the Actor model than one classically gets with just threads. [I say "close" because, in Actor model one would normally consider the other Actor to already be there, not something you start up dynamically.]

C# also has a run time interpreter (JIT compiler) but this has no construct like the Global Interpreter Lock. So it's possible for this runtime to use operating system threads for C# threads. So it has no built-in Actor model style Multi Processing classes / library, but you could write your own. Like a lot of other languages of this ilk, it has a thread pool which gets used for some operations.

The .NET C# Task Parallel library (and its DataFlow library) can do Actor model, so it will "feel" a lot like Python's multiprocessing, but it's not spawning up separate processes to do so.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.