Skip to main content
27shutterclicks
Inspiring
September 27, 2022
Answered

Lightroom SDK - Listing and killing background tasks

  • September 27, 2022
  • 1 reply
  • 947 views

#SDK - In my search to understand Lightroom SDK's LrTasks.startAsyncTask, I came across this page that discusses background processes: https://app.assembla.com/wiki/show/lrdevplugin/Background_Processes

 

It mentions: 

 

Reloading a plugin will not kill your background processes. Thus you must have a shutdown module that kills the background processes.

 

That bring about the following questions:

 

  1.  Is there a way to list running background tasks? Or in other words, how do you know if there are tasks running?
  2.  How does one go about killing tasks in general, whether it's part of the shutdown module or otherwise?

 

Thanks.

 

This topic has been closed for replies.
Correct answer johnrellis

There is no way for a plugin to enumerate its tasks or to preemptively terminate them.  The plugin will need a global boolean variable, "requestShutdown", and each long-running task will need to periodically test that variable and then exit itself.  The shutdown module simply sets that variable to true.

 

But I think it's rare that a plugin actually needs to do this. Typically, most long-running tasks are under the control of an LrProgressScope (a progress bar), and they should be testing the scope periodically to see if the user has clicked cancel.  Of all my plugins, only one has a background process not under control of a scope, and it uses the "requestShutdown" technique.

 

If a plugin has a long-running background task not controlled by a progress scope, then how important is it to be able to reload the plugin?  The Reload Plug-in command is under the Plug-in Author Tools (collapsed by default), and there's no reason for a user to invoke Reload (*).  For you the developer, if you need to reload the plugin, just restart LR instead.

 

(*) Some plugin authors have an "update plugin" command that downloads and installs new versions of the plugin automatically, and they tell the users to either Reload or restart LR.  

1 reply

johnrellis
johnrellisCorrect answer
Legend
September 27, 2022

There is no way for a plugin to enumerate its tasks or to preemptively terminate them.  The plugin will need a global boolean variable, "requestShutdown", and each long-running task will need to periodically test that variable and then exit itself.  The shutdown module simply sets that variable to true.

 

But I think it's rare that a plugin actually needs to do this. Typically, most long-running tasks are under the control of an LrProgressScope (a progress bar), and they should be testing the scope periodically to see if the user has clicked cancel.  Of all my plugins, only one has a background process not under control of a scope, and it uses the "requestShutdown" technique.

 

If a plugin has a long-running background task not controlled by a progress scope, then how important is it to be able to reload the plugin?  The Reload Plug-in command is under the Plug-in Author Tools (collapsed by default), and there's no reason for a user to invoke Reload (*).  For you the developer, if you need to reload the plugin, just restart LR instead.

 

(*) Some plugin authors have an "update plugin" command that downloads and installs new versions of the plugin automatically, and they tell the users to either Reload or restart LR.  

27shutterclicks
Inspiring
September 27, 2022

Thanks for the insight, John.

 

For my purposes, I was considering a background tasks that periodically reads the main preferences file, which seems to be written to disk only 30 seconds after the last change to it. Prompting the user to wait 30 seconds before proceeding seems impractical.

 

But the more I think about it, I think there isn't any other way around it, since even the background task wouldn't exactly solve the issue. 

 

At least I understand more the mechanism of ending a task now. Thanks.

27shutterclicks
Inspiring
September 28, 2022

With this approach in mind, I found what seems to be one of those background tasks in the Luminar Neo plugin > ExportInit.lua:

 

--g_LuminarNeo_isPluginRunning is used since async task can be running even after plug-in shutdown
g_LuminarNeo_isPluginRunning = 1

LrTasks.startAsyncTask(function()
	while g_LuminarNeo_isPluginRunning == 1 do
		if LrFileUtils.exists(importFileName) then
			local photoPath = LrFileUtils.readFile(importFileName)
			tryToImportFromFile(photoPath)
			LrFileUtils.delete(importFileName)
		end
		--sleep for 1 second
		LrTasks.sleep(1)
  end
end)

... which appears to be checking for the presence of a file in the temp folder every second?

 

I mean, I use Luminar once in a while, but it keeps checking every second for the file? Isn't it a terribly inefficient approach? I wonder how much that check every second tasks the system, especially when using a laptop on battery power?