Run a Background Job in the Current User Session

I’m trying to set up a custom-scripted bulk data import process that opens and processes quite a few files in sequence. As part of this, the system occasionally needs to prompt for new user input to confirm parameters. On the scripting side, this is handled by pausing the Python import script while waiting for the submission of a JavaScript dialog. I’ve handled the interactions between the Python and JavaScript functions by setting up variables in the frappe.local object.

In order to work around browser/server timeouts, I’ve set up the jobs for each file to queue as a separate task in the Frappe Background Jobs queue.

Here’s my issue: it appears that the background jobs are running in their own session, with a separate frappe.local object compared to the user/session that triggered the import job.

For example, I can initialize a new dict variable in the frappe.local object as follows:

if not hasattr(frappe.local, 'items'):
        frappe.local.items = {}

As long as I’m running code in the same session, I can manipulate the frappe.local.items dictionary from one function to another. If I pass the current user as an argument in the background job, I can even prompt a JS dialog for the current user by setting the user argument on emit_js:

frappe.emit_js('part_item_mapping_dialog(\'{0}\')'.format(item_name), user=user)

However, if I initialized the frappe.local.items variable from a script that’s running in a background job, I get this error when trying to access the items variable from a script that’s initiated by the current user session (on confirmation of the JavaScript dialog):

AttributeError: items

This seems to indicate that the function that runs in the current user session is operating with a separate frappe.local object compared to the function that’s running in the background job.

How can I run a background job to execute in the current user session–with the same frappe.local object–as the user session that initiated the original import job?