Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue633 threaded download #708

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

HansVRP
Copy link
Contributor

@HansVRP HansVRP commented Jan 15, 2025

Somewhat of a preemptive PR.

I tried to thread of the downloading in a similar style as the threaded MPJM.

I'd also would hope to get some input on needed unit tests. My current ideas are:

  1. to see if the thread is properly closed
  2. to see what happens when the download is interrupted
  3. to verify that multiple threads have been created.

@HansVRP HansVRP requested a review from soxofaan January 15, 2025 19:45
@HansVRP
Copy link
Contributor Author

HansVRP commented Jan 17, 2025

Secondly I want to be cautious on how this potentially interacts with export workspace.

Copy link
Member

@soxofaan soxofaan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some quick notes

def on_job_done(self, job: BatchJob, row):
"""
Handles jobs that have finished. Can be overridden to provide custom behaviour.

Default implementation downloads the results into a folder containing the title.
Default implementation runs the download in a separate thread.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think threading should be opt-in and classic serial downloading should be the default (at least for now).
It's easy to get in trouble with threading (and other parallelism paradigms like async as used in notebooks).


:param job: The job that has finished.
:param row: DataFrame row containing the job's metadata.
"""
# TODO: param `row` is never accessed in this method. Remove it? Is this intended for future use?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd keep this TODO comment, it's not resolved yet as far as I know

# Start the download in a separate thread
_log.info(f"Starting download thread for job {job.job_id}")
downloader = Thread(target=download_task, daemon=True)
downloader.start()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should keep track of the Thread objects, to properly .join() them when they are done later

def download_task():
try:
_log.info(f"Starting download for job {job.job_id} to directory {job_dir}")
job.get_results().download_files(target=job_dir)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you use job here as non-local variable from the on_job_done closure. I wonder how thread-safe this is.

A batch job object on it's own is not that complex except for the connection inside it: that's quite a complex object with a lot of state and risk of being thread-unsafe. For robust threaded downloading I would try to minimize the data structures you are sharing from the main thread to the download thread. E.g. just the backend URL (a string), the job id (a string) and a valid access token (a string) might be enough. Or even better: just pass the download URLs if they are signed (and don't require a working access token)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants