Python – Download File Using Requests, Directly to Memory

r.raw (HTTPResponse) is already a file-like object (just pass stream=True): #!/usr/bin/env python import sys import requests # $ pip install requests from PIL import Image # $ pip install pillow url = sys.argv[1] r = requests.get(url, stream=True) r.raw.decode_content = True # Content-Encoding im = Image.open(r.raw) #NOTE: it requires pillow 2.8+ print(im.format, im.mode, im.size) In general … Read more

In malloc, why use brk at all? Why not just use mmap?

Calling mmap(2) once per memory allocation is not a viable approach for a general purpose memory allocator because the allocation granularity (the smallest individual unit which may be allocated at a time) for mmap(2) is PAGESIZE (usually 4096 bytes), and because it requires a slow and complicated syscall. The allocator fast path for small allocations … Read more

Why use shm_open?

If you open and mmap() a regular file, data will end up in that file. If you just need to share a memory region, without the need to persist the data, which incurs extra I/O overhead, use shm_open(). Such a memory region would also allow you to store other kinds of objects such as mutexes … Read more

When would you use mmap

mmap can be used for a few things. First, a file-backed mapping. Instead of allocating memory with malloc and reading the file, you map the whole file into memory without explicitly reading it. Now when you read from (or write to) that memory area, the operations act on the file, transparently. Why would you want … Read more

Does malloc() use brk() or mmap()?

If we change the program to see where the malloc‘d memory is: #include <unistd.h> #include <stdio.h> #include <stdlib.h> void program_break_test() { printf(“%10p\n”, sbrk(0)); char *bl = malloc(1024 * 1024); printf(“%10p\n”, sbrk(0)); printf(“malloc’d at: %10p\n”, bl); free(bl); printf(“%10p\n”, sbrk(0)); } int main(int argc, char **argv) { program_break_test(); return 0; } It’s perhaps a bit clearer that … Read more

What is the purpose of MAP_ANONYMOUS flag in mmap system call?

Anonymous mappings can be pictured as a zeroized virtual file. Anonymous mappings are simply large, zero-filled blocks of memory ready for use. These mappings reside outside of the heap, thus do not contribute to data segment fragmentation. MAP_ANONYMOUS + MAP_PRIVATE: every call creates a distinct mapping children inherit parent’s mappings childrens’ writes on the inherited … Read more

Speeding up file I/O: mmap() vs. read()

Reads back to what? What is the final destination of this data? Since it sounds like you are completely IO bound, mmap and read should make no difference. The interesting part is in how you get the data to your receiver. Assuming you’re putting this data to a pipe, I recommend you just dump the … Read more