I am creating an application which must execute a function that takes too long (lets call it slowfunc()
), which is a problem, since my applicati开发者_如何转开发on is working with a live video feed. By running this function every frame, the frame rate is severely affected.
Is there a way to run slowfunc()
in the background without using threading? I don't necessarily need it to run every frame, but every time it finishes, I'd like to examine the output. The only thing I can think of right now is to split up slowfunc()
into several "mini-functions" which would each take approximately an equal amount of time, then run one minifunction per frame. However, slowfunc()
is a relatively complex function, and I feel that there should be (hopefully is) a way to do this simply.
EDIT: I can't use threading because this program will eventually be used on a tiny robot processor which probably will not support threading. I guess I can use "cooperative multitasking". Thanks for your help!
Run it in a thread and after the calculation is finished, make the thread sleep until another calculation is ready to be run. That way you are not hit with the initialization of the thread every time.
You're asking for simaltaneous execution. The two ways to do this are -: a) Multi-threading -: Create another thread to run on a background. b) MUlti-processing -: Create another process . Take all inputs required for the function via a shared memory model. Create a synchronisation mechanism with original process(parent process). Execute this function.
It is normally prefered to use the 1st one. Faster execution.
The 2nd one guarantees that if the function crashes your parent process still runs. Although, that is a bit irrelevant since, why would you want your child(function) to crash. This needs more memory.
精彩评论