My Raspberry Pi 3 b+ is reading by Modbus the values of an irradiation sensor, processing all the data, showing it in a plot and showing some warnings (if the irradiation ramp is higher than 5%).

I am computing all this in a Python process but it takes too much time between every sample I can read from the sensor. I would like to run every task in a different thread:

Thread 1: Reads the data using Modbus and stores it in a vector/file or whatever

Thread 2: Reads the data from the vector/file and plots it

Thread 3: Reads the data from the vector/file and computes the ramp

I have read about two options:

  1. using threading library
  2. using multiprocessing package

First option the threads will use the same memory space, so it's gonna be more easy to share information between them. But I am not sure that every single thread will run in a different core of the Raspberry or they will share the same core and be syncroniced to access the core by the OS.

Second option the process won't share the space memory so the share of the information between process will be harder.I am also not sure that every single process will run in a different core of the Raspberry or they will share the same core and be syncroniced to access the core by the OS.

I also think that maybe Python is not the best option and this should be made with C.

Does anyone can give any clue about what to do? Or give me any literature I can check about it?

  • How long is it currently taking between samples? How fast do you need the samples to be taken?
    – CoderMike
    Feb 8, 2019 at 16:18
  • 1
    Might help if you include your current Python code (in a code block).
    – CoderMike
    Feb 8, 2019 at 16:29
  • @Dani I think you do not need to change your programming language, you could get more performance with using threading + asyncio or processing + asyncio. I posted an answer with more detailed information. Feb 8, 2019 at 19:52
  • This is a programming question and has nothing to do with Raspberry Pi. You should better ask at stackoverflow.com.
    – Ingo
    Feb 8, 2019 at 23:07
  • The anwer from Benyamin Jafari answers your question. It would be very nice if you could accept it with a click on the tick on its left side. This will finish your question and it does not pop up year by year.
    – Ingo
    Nov 6, 2019 at 11:22

1 Answer 1


In the Python language, there isn't a major difference between process or thread, because it handles by OS (i.e in Linux using fork()), but the main difference between them is that the threads run in the same memory space while processes have separate memory.


If you want to do several tasks as concurrency, you have three choices:

  • Multiprocessing: multiprocessing library
  • Multithreading: threading library
  • Asynchronization: asyncio, twisted, tornado libararies.

Asynchronous tasks usually are advantages and are optimized for I/O tasks.

I suggest if you are using Python3, the asyncio event-loop is appropriate to reading Modbus protocol, which is supported by Asyncrounus pymodbus library.

[Other Answers]:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.