0

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 at 16:18
  • 1
    Might help if you include your current Python code (in a code block). – CoderMike Feb 8 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. – Benyamin Jafari Feb 8 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 at 23:07
0

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.


[NOTE]:

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 acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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