r/tasker • • Oct 26 '26

Task collision and single threading

Hi,

I have a profile that intercepts notifications from an app and fires off a task to process the notification contents.

The app in question can issue several notifications at the same time. How can I ensure that the triggered task is single threaded, with only one task instance running at a time while at the same time getting the correct information from the notification that it is processing. 

There is an option in task settings to 'Run Both Together' but I don't want the task activity to be interlaced and anyway there could be more than 2 instances at play here. I'm looking at setting up a lock/unlock gate mechanism at the start and end of the task, but not sure of the exact best way to approach this (or if there is another way to guarantee task single threading).

Appreciate any advice.

TIA.

2 Upvotes

14 comments sorted by

2

u/The_IMPERIAL_One S24 Ultra (A16) | realme GT NEO 3 (A14) Oct 26 '26

Collision Handling > Abort New Task

Does this not work? Or is it not sufficient?

2

u/dougwzz99 Oct 26 '26

Thanks. I think if I specify that then I run the risk of one or more of the multiple notifications not being processed as one or more of the tasks fired up to process it will be aborted. I'm looking for a way to guarantee that all of tasks will run but each instance will run by itself with the others waiting (in a queue?) for the running instance to finish.

2

u/The_IMPERIAL_One S24 Ultra (A16) | realme GT NEO 3 (A14) Oct 26 '26

Got it. I might have missed this.

You can make use of Java Code action (LLMs could help too, go to the action and copy system instructions).

2

u/DylanXu054728 Oct 27 '26

Abort New Task on its own will drop the data from the 2nd and 3rd notification, since those task instances just get killed. What you want is a queue: the profile task only records the notification, and a separate task consumes the queue one item at a time.

Profile task (Enqueue), collision Run Both Together:

  1. Array Push: variable %NotifQueue, position 9999, value %antitle||%antext (or whatever fields you need, with a separator you can split on later)
  2. Perform Task: Process Queue

Process Queue task, collision Abort New Task:

  1. Array Pop: variable %NotifQueue, position 1, To Var %item
  2. Variable Split %item with || and do your processing
  3. Goto action 1 if %NotifQueue(#) > 0

Every notification gets pushed, and only one Process Queue ever runs. If it's already running, the new Perform Task is aborted but the loop still picks up the item because it re-checks the array before exiting.

Notes: the queue has to be global (capital letter in the name) so both tasks see it. There's a tiny window where an item is pushed right after the final Goto check; if that matters, add Wait 1 second and the same check once more before the task ends.

1

u/Exciting-Compote5680 Oct 27 '26

Aren't you just moving the bottleneck from one task to another? Tasks are already queued, this just adds more overhead. 

2

u/DylanXu054728 Oct 27 '26

Fair question, but they aren't queued as whole tasks. The Scheduling section on the Tasks page of the userguide says instances with the same priority take turns executing one action each, starting with the most recently added one. So with Run Both Together and three notifications arriving at once, you get three copies interleaved action by action, with the newest one going first. That's the interlacing OP said they wanted to avoid.

With the array, each notification is processed start to finish before the next one, in the order they arrived. The extra cost is one push, one pop and one Goto per notification, which is nothing next to the actual processing.

If the processing is just a couple of quick actions with no Wait or HTTP Request in it, the interleaving probably won't matter and plain Run Both Together is fine.

1

u/dougwzz99 Oct 27 '26 edited Oct 28 '26

I'm already using the approach that you outlined with the notification profile task simply doing a PUSH with the required information into a global array. A seperate profile then triggers when it detects an entry to the array, and the associated task then does the POP and processes the array element, looping until the array is empty.

My concern was that even the simple task doing the PUSH action would have issues if it was triggered multiple times in quick succession, and I also wasn't sure about any interlacing that might result from Run Both Together.

This discussion and some testing I've done has convinced me that I was overthinking this :-) Any interlacing that may or may not occur is irrelevant with the separation of processing provided by the PUSH/POP approach.

Thanks to all for the assistance.

1

u/Exciting-Compote5680 Oct 27 '26

I thought tasks launched by the same profile were always executed in order, with the next run only starting when the previous has completed (unless 'Enforce Task Order' is disabled). But the queuing task might help avoid hitting the queue limit by acting as a buffer. Might need to call the processing task with lower priority though. 

1

u/Exciting-Compote5680 Oct 26 '26

As far as I know tasks are performed in a single queue ('Run Both Together' just means the second instance will run after the first is completed, assuming they are running with equal priority). What works best for your use case depends mostly on the specific situation/information/notifications, or whatever 'the correct information' means for your use case (first notification? last notification?). The issue you can run into with 'Run Both Together' is that the number of queued tasks exceeds the maximum queue length and any task over the limit is ignored. If it is just by a couple (and the tasks are short/fast) you could increase that number in Preferences. But using 'Abort New Task' or 'Abort Existing Task' always means you are ignoring some notifications. If that works for your use case (for instance because the latest notification always has the most recent and complete information) you have less risk of clogging the queue. If you think you need some sort of mechanism, you could perhaps look at %TRUN ("A comma-separated list of any named tasks which are currently running. The list always starts and ends with a comma to make matching easier, if it's not empty."). But I think you are going to run into the same tradeoff: either you have a lot of notifications to process, or you discard some of them. 

1

u/dougwzz99 Oct 26 '26

Thanks, I think 'Run Both Together' is the way to go here, but I wasn't sure if this would create a queue of the triggered tasks with each running on its own as and when it got to the top of the queue. Maybe a combination of 'Run Both Together' and 'Enforce Task Order' (in the Profile settings) will do what I want, but I wasn't certain which is why I was looking at writing my own lock mechanism.

I'll try setting up some test scenarios with multiple notifications to see if I can confirm if this works or not.

1

u/Rich_D_sr Oct 27 '26

1

u/dougwzz99 Oct 27 '26

Thanks, thats a VERY interesting read!

1

u/Rich_D_sr Oct 27 '26

You should be able to achieve your goal using method mentioned in section 8. Post back with any questions.

1

u/sasreedit S22, GW5P Nov 01 '26

A different thought here... Are the notifications received all the same title & text? If no, maybe identify which is fired first and simply react to that notification only.

You can discover by using AN and a text file to see all the notifications.