-
Notifications
You must be signed in to change notification settings - Fork 74
[SYNPY-1508]: add tutorial for downloading files by Synapse ID concurrenty #1337
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
Open
thomasyu888
wants to merge
1
commit into
develop
Choose a base branch
from
SYNPY-1508-tutorial-loose-file
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| [](){ #tutorial-downloading-a-file } | ||
| # Downloading files by Synapse ID | ||
|
|
||
| This tutorial shows how to download any set of files from Synapse using their | ||
| Synapse IDs. Rather than syncing an entire project or folder, this approach lets | ||
| you target exactly the files you need and download them **concurrently** — even | ||
| directing each file to a different local directory. | ||
|
|
||
|
|
||
| ## Tutorial Purpose | ||
| In this tutorial you will: | ||
|
|
||
| 1. Build a mapping of Synapse IDs to local download directories | ||
| 1. Download all files concurrently using the async API | ||
|
|
||
|
|
||
| ## Prerequisites | ||
| * Make sure that you have completed the following tutorials: | ||
| * [Folder](./folder.md) | ||
| * [File](./file.md) | ||
| * The target directories (`~/temp/subdir1`, etc.) must exist before running the | ||
| script. Create them or replace them with directories of your choice. | ||
|
|
||
|
|
||
| ## 1. Build a mapping of Synapse IDs to download directories | ||
|
|
||
| Create a dictionary that maps each Synapse ID to the local path where that file | ||
| should be saved. Files can be directed to different directories as needed. | ||
|
|
||
| ```python | ||
| {!docs/tutorials/python/tutorial_scripts/download_file.py!lines=13-30} | ||
| ``` | ||
|
|
||
|
|
||
| ## 2. Download all files concurrently | ||
|
|
||
| Use `File.get_async()` together with `asyncio.gather` to kick off every download | ||
| at the same time and wait for them all to finish. | ||
|
|
||
| ```python | ||
| {!docs/tutorials/python/tutorial_scripts/download_file.py!lines=33-42} | ||
| ``` | ||
|
|
||
| <details class="example"> | ||
| <summary>After all downloads finish you'll see output like:</summary> | ||
| ``` | ||
| Retrieved 12 files | ||
| ``` | ||
| </details> | ||
|
|
||
|
|
||
| ## Source code for this tutorial | ||
|
|
||
| <details class="quote"> | ||
| <summary>Click to show me</summary> | ||
|
|
||
| ```python | ||
| {!docs/tutorials/python/tutorial_scripts/download_file.py!} | ||
| ``` | ||
| </details> | ||
|
|
||
| ## References used in this tutorial | ||
|
|
||
| - [File][synapseclient.models.File] | ||
| - [File.get_async][synapseclient.models.File.get_async] | ||
| - [syn.login][synapseclient.Synapse.login] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| """ | ||
| Here is where you'll find the code for the downloading a file tutorial. | ||
| """ | ||
|
|
||
| import asyncio | ||
|
|
||
| from synapseclient import Synapse | ||
| from synapseclient.models import File | ||
|
|
||
| syn = Synapse() | ||
| syn.login() | ||
|
|
||
| # A mapping of Synapse IDs to the local directory each file should be downloaded to. | ||
| # Files can be directed to different directories as needed. | ||
| SYN_IDS_AND_PATHS = { | ||
| "syn60584250": "~/temp/subdir1", | ||
| "syn60584256": "~/temp/subdir1", | ||
| "syn60584248": "~/temp/subdir1", | ||
| "syn60584252": "~/temp/subdir1", | ||
| "syn60584258": "~/temp/subdir1", | ||
| "syn60584260": "~/temp/subdir1", | ||
| "syn60584257": "~/temp/subdir1", | ||
| "syn60584251": "~/temp/subdir1", | ||
| "syn60584253": "~/temp/subdir1", | ||
| "syn60584390": "~/temp/subdir1", | ||
| "syn60584405": "~/temp/subdir2", | ||
| "syn60584400": "~/temp/subdir3", | ||
| } | ||
|
|
||
|
|
||
| async def main(): | ||
| # Build a list of concurrent download tasks — one per Synapse ID | ||
| tasks = [] | ||
| for syn_id, path in SYN_IDS_AND_PATHS.items(): | ||
| tasks.append(File(id=syn_id, path=path).get_async()) | ||
|
|
||
| # Download all files concurrently and wait for every one to finish | ||
| results = await asyncio.gather(*tasks) | ||
|
|
||
| print(f"Retrieved {len(results)} files") | ||
|
|
||
|
|
||
| asyncio.run(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you missed by one line:
