Run a cloud function as a separate thread?

So this may be a bit of a strange request, but is it possible to run cloud registered functions in a separate thread (that is to say, separate from the application thread process)? I know normal functions can now be run separately in their own threads but I’m assuming this does not yet apply to cloud functions (I wasn’t able to find any forum posts relating to this).

Here’s my situation: The photon is acting as a web interface for a much more complex device–as such, there isn’t enough space or resources to store all necessary data inside the photon itself, it has to be polled from our device via the photon with remote function calls on-demand. This means we call a cloud function remotely for a single block of data, wait for the communication reply from our primary device and then return that block to the cloud and then the web server initiates another function call for the next block, etc.

The problem is this remote query call may take anywhere from ~100ms (if successfully received and processed the first time) up to 1 second (if there are problems / interference with communicating with the main device–also sometimes multiple messages must be exchanged between the devices, etc). Since cloud functions get executed between Loop() calls this equates to sizable delays in the application code. Really even a 100ms delay between loops of the application code is unideal (though not unworkable). Obviously larger delays like a full second in application code presents much more significant problems though since we do other time sensitive tasks in application code.

We could just have the function call publish an event when it’s complete and then call a different function (or read a cloud variable status) at that point, but that limits us pretty significantly in how fast we can poll data from the device (1 published event per second) plus it greatly increases latency and bandwidth to a lesser extent. Full data transfers could be more than a megabyte depending on the device state so we’d be looking at a very long transfer time if we did things this way.

Ideally we’d set it up so the cloud function (we’ll refer to it as “GetDeviceData”) runs independently from application code to avoid blocking while communicating with the device and simply returns its payload whenever it finishes, eliminating the need for a follow up function call, event publishing or variable polling.

Is this possible / beneficial to others? What are other peoples thoughts?

A cloud function can only return a single integer. If your device data is so large (1 Mb), you will want to send it in larger chunks than that. I would implement “GetDeviceData” as a Particle.function that sends the actual data in the optional data argument of Particle.publish.

If you want to poll a smaller amount of data at a regular interval, your firmware code could continuously update a string Particle.variable. Your website or app can then poll the value of the variable every second, half second or whatever your application requires.