how to handle long processing jobs? is there a way to start a background thread in Corona?

Hi,

I’m trying to port a game I developed for iOS with Apple framework to Corona and Lua in order to have better performance and to be able to release it on Android too.

In my game I have a method called before every frame update where I do all the calculations I need at real time like updating the player position, etc. I now I can do the same in Corona with Runtime:addEventListener(“enterFrame”, myMethod).

There are other tasks I need to perform in an asynchronous way, for example I need to make some calculations for adding tracks to the road the player is running on. I divided these calculations in smaller methods but some of them are still too slow and just can’t complete between two frames so I start them on background threads (dispatch_async) knowing I will have them finished when I will need the results.

How can I do something like that in Corona? I can’t find I way to start a job in a background thread up to now. 

I looked at coroutines but I can’t find a way to use them for executing a background job since they just stop the main thread until they yield the control back to it. 

Thank you

There may be a much better answer to this but I believe timer.performWithDelay makes a new thread.

Corona is a single-threaded SDK. Below is a forum post with some in-depth info from developers whom are well-versed with squeezing every ounce of performance from Corona:

https://forums.coronalabs.com/topic/61514-threading-in-corona/

By the way, the library I mention in Alex’s thread has since been submitted as a plugin, along with a serialization library (meant in part to complement it, giving you a few ways to pass structured data back and forth, if JSON doesn’t suit you); just a waiting game at this point, barring any major problems.

It won’t let you use Corona-specific functionality, e.g. creating display objects, from other processes, but data processing should be ideal.

Thank you very much to you all,

it seems it isn’t as simple as I hoped. I will definetely learn about coroutines as much as possible and maybe also about luaproc.

this tutorial seems great by the way.

The thread created by the timer will always be interrupted by the enterFrame event? I mean, is it always running at a lower priority by design? if it is so may this be also a good way to execute a long processing task in an asynchronous way?

API documentation don’t explain too much about performWithDelay, it doesn’t even tell if a new thread is created or not.

timer.performWithDelay() does not create a new thread.

There may be a much better answer to this but I believe timer.performWithDelay makes a new thread.

Corona is a single-threaded SDK. Below is a forum post with some in-depth info from developers whom are well-versed with squeezing every ounce of performance from Corona:

https://forums.coronalabs.com/topic/61514-threading-in-corona/

By the way, the library I mention in Alex’s thread has since been submitted as a plugin, along with a serialization library (meant in part to complement it, giving you a few ways to pass structured data back and forth, if JSON doesn’t suit you); just a waiting game at this point, barring any major problems.

It won’t let you use Corona-specific functionality, e.g. creating display objects, from other processes, but data processing should be ideal.

Thank you very much to you all,

it seems it isn’t as simple as I hoped. I will definetely learn about coroutines as much as possible and maybe also about luaproc.

this tutorial seems great by the way.

The thread created by the timer will always be interrupted by the enterFrame event? I mean, is it always running at a lower priority by design? if it is so may this be also a good way to execute a long processing task in an asynchronous way?

API documentation don’t explain too much about performWithDelay, it doesn’t even tell if a new thread is created or not.

timer.performWithDelay() does not create a new thread.