Are Cloud function calls asynchronically on the device?


I have begun working on concepts for firmware design. I am trying to understand how Particle.function() and Particle.subscribe() works in the background.

Are calls from the Cloud being executed asynchronically through the FreeRTOS on the Particle Device? I am working with Serial and multiple clients can access the Device. Currently there are two concepts:

  1. Having a Cloud services in the Background that acts as a gateway between multiple clients and the Particle Device. When a client requests data from the device or runs a command on the device it will be queued by the gateway so that only one active connection will be made to the particle device.

  2. Since working with Serial on the Device I am using WITH_LOCK(Serial) on the device to make sure that a call / request will only access serial when it’s not already being accessed.

No. 2 is by far the most elegant since this does not require an additional background server running that gateway. BUT …

This means that both Particle.function() and Particle.subscribe() will be called asynchronically. Is this the case? I cannot find any info in the docs!




I was just thinking that maybe the Particle Cloud Backend is already acting as a Gateway. This is the case when multiple clients to subscribe to an event published by a device.

But what happens when multiple clients call a function on a device and this function has blocking code? Is this function executed asynchronically or does the Particle Cloud Backend queue this call up?

And what happens when a device subscribes to a topic that is being publish by multiple devices? Is this also being executed asynchronically?


Let me ping one of the Elites that might be able to help, @ScruffR do you have any insight on this?


I’m not entirely sure how things behave nowadays, but my last info was, that Particle.function() and Particle.subscribe() tasks are serviced by the application thread, no matter if SYSTEM_THREAD(ENABLED) or not.
Hence when not calling Particle.process() in MANUAL mode even with the system thread on, these would not be serviced.

The requests from the cloud come in asyncronously and are queued by the system thread, and while Particle.variable()s are handled immediately by the system thread, Particle.function()s and Particle.subscribe()s are handled on next Particle.process() (or implicitly by dropping out of loop() or delay() or …) one at a time in a FCFS manner.

One thing that used to be an issue (and I’m not sure about the status of that) with that behaviour is that when hitting the device with lots of events which it has subscribed to, the payload buffer might be overwritten by a subsequent event of the same filter setting, if your code does not handle the subscriptions often and quick enough.

1 Like