Is multiprocessing supported?

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
Kc2021
 
Posts: 28
Joined: Mon Jul 19, 2021 11:42 am

Is multiprocessing supported?

Post by Kc2021 »

I have tested several adafruit products, they are all great quality. However they all seems to present the same problem when use with the multiprocessing module. The script dose not run and throws a Segmentation fault (core dumped). The script runs with threading but not multiprocessing.

These does not works:

Code: Select all

import time
import board
import adafruit_bno055
import threading
import multiprocessing

fpsFilt = 0
timeStamp = 0

i2c = board.I2C()
sensor = adafruit_bno055.BNO055_I2C(i2c)

def test():
    while True:
        print("Quaternion: {}".format(sensor.quaternion))
   
Gps = multiprocessing.Process(target=test)

Gps.start()
But these works:

Code: Select all

import time
import board
import adafruit_bno055
import threading
import multiprocessing

fpsFilt = 0
timeStamp = 0

i2c = board.I2C()
sensor = adafruit_bno055.BNO055_I2C(i2c)

def test():
    while True:
        print("Quaternion: {}".format(sensor.quaternion))
   
Gps = threading.Thread(target=test)

Gps.start()
Is there any way to use an adafruit product with multiprocessing?Thanks.
Last edited by dastels on Sat Jul 02, 2022 10:05 pm, edited 1 time in total.
Reason: Add code tags

User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: Is multiprocessing supported?

Post by dastels »

What boards are you trying these on?

Dave

User avatar
blakebr
 
Posts: 942
Joined: Tue Apr 17, 2012 6:23 pm

Re: Is multiprocessing supported?

Post by blakebr »

Tracking

User avatar
Kc2021
 
Posts: 28
Joined: Mon Jul 19, 2021 11:42 am

Re: Is multiprocessing supported?

Post by Kc2021 »

I am using a PA1010D connected to a linux 18 pc with MCP2221A through the stemma qt port.
This also happens with BNO055, and Servo controller like I2C interface - PCA9685. The problem arises only when the while loop is in a multiprocessing module. I am working in a very cool project and I need to get data from several adafruit sensors at the same time without having the slower refresh rate sensors slow down the faster sensors. So I want to put every sensor loop in a different process.
Where you able to reproduce the error?

User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: Is multiprocessing supported?

Post by dastels »

I should have been more specific: what are you running the code on?

Dave

User avatar
Kc2021
 
Posts: 28
Joined: Mon Jul 19, 2021 11:42 am

Re: Is multiprocessing supported?

Post by Kc2021 »

Thankyou for addressing my code problem. I think I don't fully understand your question. Please be patient with me, when you say "what are you running the code on?
" what do you mean?

If an example like these where to be followed and the while loop placed inside a def and converted to a process will it produce an error?
https://learn.adafruit.com/circuitpytho ... p2221/gpio

For me every code with a multiprocessing.Process produces an error.

User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: Is multiprocessing supported?

Post by dastels »

I mean are you running it on a Raspberry Pi? a SAMD51 (aka "M4") board?

Dave

User avatar
Kc2021
 
Posts: 28
Joined: Mon Jul 19, 2021 11:42 am

Re: Is multiprocessing supported?

Post by Kc2021 »

I am using a regular pc with ubuntu 20 installed, I use the MCP2221A as an usb to ic2 interphase.

User avatar
Kc2021
 
Posts: 28
Joined: Mon Jul 19, 2021 11:42 am

Re: Is multiprocessing supported?

Post by Kc2021 »

the set up is like this but running Linux and a script.
https://www.adafruit.com/product/4471

User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: Is multiprocessing supported?

Post by dastels »

OK. There may be some race conditions or resource contention or non-atomic ordering issues using the USB/I2C adaptor.

Can you check that access to the device is atomic? If not, you could add a wrapper with a semaphore approach to make it atomic.

Dave

User avatar
Kc2021
 
Posts: 28
Joined: Mon Jul 19, 2021 11:42 am

Re: Is multiprocessing supported?

Post by Kc2021 »

It is the first time I encounter the term "atomic". Could you give me an example of a wrapper with a semaphore approach? I am researching about is now.

User avatar
gammaburst
 
Posts: 1013
Joined: Thu Dec 31, 2015 12:06 pm

Re: Is multiprocessing supported?

Post by gammaburst »

By the way, after you solve the multiprocessing problem, beware the BNO055 is antisocial. It uses I2C clock-stretching to hog the bus for a couple hundred microseconds at a time, restricting your ability to talk fast to multiple devices.

User avatar
Kc2021
 
Posts: 28
Joined: Mon Jul 19, 2021 11:42 am

Re: Is multiprocessing supported?

Post by Kc2021 »

I can use another MCP2221A to handle BNO055, but I will have to find out how to use 2 MCP2221A in the same pc without problems.
Have anybody reproduce the error? It is very simple to reproduce. Just get the while loop inside the multiprocessing module.

User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: Is multiprocessing supported?

Post by dastels »

You want to look into Mutex and it's uses. The key is that when something (i.e. a process/thread) want to use the I2C adapter it has exclusive access to it until it's done. Then something else can have it.

Dave

User avatar
Kc2021
 
Posts: 28
Joined: Mon Jul 19, 2021 11:42 am

Re: Is multiprocessing supported?

Post by Kc2021 »

Ok I will research that.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”