Skip to main content

asyncio extension of PycURL

Project description

handle = aiocurl.Curl()
handle.setopt(aiocurl.URL, 'https://example.com')

await handle.perform()

Note: currently this requires a patched version of PycURL!

How?

Using libcurl's socket_action interface to let asyncio's event loop do all the work of waiting for I/O and scheduling of timeouts.

multi_socket supports multiple parallel transfers — all done in the same single thread — and have been used to run several tens of thousands of transfers in a single application. It is usually the API that makes the most sense if you do a large number (>100 or so) of parallel transfers.

This setup allows clients to scale up the number of simultaneous transfers much higher than with other systems, and still maintain good performance. The "regular" APIs otherwise waste far too much time scanning through lists of all the sockets.

More examples?

Awaiting multiple transfers

Use any of asyncio's functions:

await asyncio.gather(
    handle1.perform(),
    handle2.perform(),
)

Even better:

multi = aiocurl.CurlMulti()

await asyncio.gather(
    multi.perform(handle1),
    multi.perform(handle2),
)

Advantages of using a multi handle:

  • connection reuse
  • multiplexing
  • shared SSL session and DNS cache

Pausing and resuming a transfer

Simply use the existing pause method:

handle.pause(aiocurl.PAUSE_ALL)

And to resume:

handle.pause(aiocurl.PAUSE_CONT)

For more pause options see libcurl's documention.

Stopping a tranfer

The opposite of perform:

handle.stop()

And if the transfer is performed by a multi handle:

multi.stop(handle)

A stopped perform will return None instead of the finished handle:

if await handle.perform():
    print('finished')
else:
    print('stopped')

Dependencies?

  1. PycURL 7.43.0.4 or above. It has essential fixes that make event-driven transfers work. Older releases fail to relay libcurl's event messages.
  2. PycURL subclass patch. It makes extending of PycURL's classes possible. You can find the patch in this branch.
  3. (optional) Additional PycURL event-related fixes that make pausing and resuming of transfers work.

For now consider this module as a simple demonstration of what these PycURL fixes and additions make possible.

License

aiocurl - asyncio extension of PycURL
Copyright (C) 2021  fsbs

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiocurl-0.0.1.post1.tar.gz (16.0 kB view details)

Uploaded Source

File details

Details for the file aiocurl-0.0.1.post1.tar.gz.

File metadata

  • Download URL: aiocurl-0.0.1.post1.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for aiocurl-0.0.1.post1.tar.gz
Algorithm Hash digest
SHA256 4d2841415e984e3bba1b9109b1475cdcbb98fd2230a98f1d179db989f82c0385
MD5 d3c180c6f1e56505e7c1b85dab178d41
BLAKE2b-256 dcabe93a5d6fe1d8fd31ddf13a97960334d2a3bf03ee5551d93297ffc8734692

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page