-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3111 from OpenNeuroOrg/asgi-support
Migrate worker to ASGI / uvicorn
- Loading branch information
Showing
43 changed files
with
963 additions
and
674 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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,2 @@ | ||
# Default size for Linux is 65536 but other factors may require tuning this | ||
CHUNK_SIZE_BYTES = 65536 |
This file contains 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 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 |
---|---|---|
@@ -1,36 +1,22 @@ | ||
import os | ||
import tempfile | ||
import zlib | ||
import aiofiles | ||
|
||
CHUNK_SIZE_BYTES = 2048 | ||
from datalad_service.common.const import CHUNK_SIZE_BYTES | ||
|
||
|
||
def update_file(path, stream): | ||
async def update_file(path, stream): | ||
"""Atomically update a file on disk with a path and source stream.""" | ||
# Delete is disabled here because we want to keep the file without double linking it | ||
with tempfile.NamedTemporaryFile(dir=os.path.dirname(path), delete=False) as tmp: | ||
async with aiofiles.tempfile.NamedTemporaryFile(dir=os.path.dirname(path), delete=False) as tmp: | ||
try: | ||
# Stream file to disk | ||
while True: | ||
chunk = stream.read(CHUNK_SIZE_BYTES) | ||
chunk = await stream.read(CHUNK_SIZE_BYTES) | ||
if not chunk: | ||
break | ||
tmp.write(chunk) | ||
await tmp.write(chunk) | ||
# Done streaming, replace the file | ||
os.replace(tmp.name, path) | ||
except: | ||
# Only remove in the failure case, we want the file if the rest succeeds | ||
os.remove(tmp.name) | ||
raise | ||
|
||
|
||
def pipe_chunks(reader, writer, gzipped=False): | ||
# If gzipped, we have to read the entire request and write once | ||
if gzipped: | ||
writer.write(zlib.decompress(reader.read(), zlib.MAX_WBITS|32)) | ||
else: | ||
while True: | ||
chunk = reader.read(CHUNK_SIZE_BYTES) | ||
if not chunk: | ||
break | ||
writer.write(chunk) |
This file contains 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 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 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 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.