User avatar
RogerW
Posts: 284
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

multiple pigpio pi objects

Sat Apr 28, 2018 9:06 am

I have a number of classes to control objects using pigpio. For example H bridge motor controller, Hitachi LCD and MCP3008 ADC. I have gone to some trouble to make all class objects use the same instance of pigpio.pi on the basis that that would set up fewer connections to the remote raspberry pi. Am I right to expect an efficiency saving or would it be better to adopt the simple solution of multiple pigpio.pi objects - one for each instance of my class objects.
Opinions please.

User avatar
DougieLawson
Posts: 35581
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: multiple pigpio pi objects

Sat Apr 28, 2018 9:31 am

I'd use a unique software object for each hardware object. One for your H-bridge, one for your LCD, one for your MCP3008.

Code: Select all

hBridge = pigpio.pi()
lcd = pigpio.pi()
mcp3008 = pigpio.pi()
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
RogerW
Posts: 284
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: multiple pigpio pi objects

Mon Apr 30, 2018 9:14 am

Thanks for the comment Dougie. In the end I opted for the easy way out and passed the pi object through the constructor. This means I can have local and remote objects but puts the onus on caller to create and stop the pi objects. It also meant fewer changes to my code.
I would still like to know if creating lots of pi objects would be less efficient.

User avatar
joan
Posts: 14093
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: multiple pigpio pi objects

Mon Apr 30, 2018 9:24 am

The pigpio demon has quite a low limit for the maximum simultaneous connections (Python pi instances). I think it is 32. You need to keep that in mind.

You could check what gpiozero does. See if they use one or multiple instances for the various devices if pigpio is the backend.

User avatar
RogerW
Posts: 284
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: multiple pigpio pi objects

Mon Apr 30, 2018 9:28 am

Thanks Joan.

Mustafa Wahhudi
Posts: 3
Joined: Thu Nov 09, 2017 11:43 pm

Re: multiple pigpio pi objects

Wed Nov 07, 2018 4:15 pm

Hello. Is it possible for multiple instances of pigpio.pi (within the same Python code) to access the same GPIO port? Will the last assigned state take effect? or there will be an error?

User avatar
joan
Posts: 14093
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: multiple pigpio pi objects

Wed Nov 07, 2018 4:19 pm

Mustafa Wahhudi wrote:
Wed Nov 07, 2018 4:15 pm
Hello. Is it possible for multiple instances of pigpio.pi (within the same Python code) to access the same GPIO port? Will the last assigned state take effect? or there will be an error?
Yes, multiple pi objects can access the same GPIO. It doesn't matter if they are in the same Python script, or in different Python scripts, or on different Pis.

If the usage is conflicting the last (in time) assignment is the one which applies.

Mustafa Wahhudi
Posts: 3
Joined: Thu Nov 09, 2017 11:43 pm

Re: multiple pigpio pi objects

Wed Nov 07, 2018 4:22 pm

Thanks joan for your quick reply :)

Return to “Python”