Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

threads vs. processes in Python

I'm about to start a program using Python which is mostly doing polling, it will constantly read from the serial port (via PySerial) and read from a file descriptor which will be changing from time to time. I started looking into the threading module but then I kept finding more and more suggestions of using the multiprocessing module instead.

I'm not well versed in Python, coming from a mostly C background. What are the technical advantages of a threaded approach in Python?

In C, threads share data vs. having to set up some IPC to communicate, that seems to be the same for Python?

My usecase:

Main process (or thread?) -
   start & initialize
       |
       V
    spaw child----------------------> start & initialize
       |                                   |
       V                                   V
      while (1) <------------+          wait for data<------+
       |                     |             |                |
       V                     |             V                |
   read file descriptors     |         read from            |
       |                     |         serial port<-----+   |
       V                     |             |            |   |
   value changed?  ------No--+             V            |   |
       |                     ^        message done?--No-+   |
       V                     |             |                |
    Report change------------+             V                |
     over serial                       alert parent---------+

So I was thinking threads, since it will make sharing data got over serial easier, and they can have a shared handle to the serial port. Does this make sense, or am I thinking about this incorrectly from a Pythonic point of view?

like image 576
Mike Avatar asked Oct 22 '12 14:10

Mike


People also ask

What is the difference between thread and process in Python?

A thread shares information like data segment, code segment, files etc. with its peer threads while it contains its own registers, stack, counter etc. A process is a program under execution i.e an active program. A thread is a lightweight process that can be managed independently by a scheduler.

What's the difference between thread and process?

A process is an instance of a program that is being executed or processed. Thread is a segment of a process or a lightweight process that is managed by the scheduler independently. Processes are independent of each other and hence don't share a memory or other resources. Threads are interdependent and share memory.

Is it better to use threads or processes?

Inter-thread communicationsInter-thread communication is far more efficient and easier to use than inter-process communication. Because all threads within a process share the same address space, they need not use shared memory.

Is Python good for multithreading?

Python doesn't support multi-threading because Python on the Cpython interpreter does not support true multi-core execution via multithreading. However, Python does have a threading library. The GIL does not prevent threading.


1 Answers

multiprocessing is mainly used in Python to avoid the GIL (Global Interpreter Lock), which stops threads being useful for trying to compute in parallel - for resource access, threads are perfect, and the better option for ease of implementation.

The GIL means that only one thread can operate on any Python object at the same time. This means that where you are trying to speed up computations, processes will be hamstrung by the other threads. In your use case, one thread will just be checking for new input, so this won't cause any problems at all.

like image 197
Gareth Latty Avatar answered Oct 05 '22 12:10

Gareth Latty