Async wrapper for requests / aiohttp, and some python crawler toolkits. Let synchronization code enjoy the performance of asynchronous programming. Read more: https://github.com/ClericPy/torequests.
Project description
Welcome to torequests’s documentation!
Quickstart
To start:
pip install torequests -Urequirements:
requestsfutures # python2aiohttp >= 3.0.5 # python3uvloop # python3optional:
jsonpath_rw_extlxmlobjectpathpsutilfuzzywuzzypython-Levenshteinpyperclip
Examples:
1. Async, threads - make functions asynchronous
from torequests.main import Async, threads
import time
def use_submit(i):
time.sleep(i)
result = 'use_submit: %s' % i
print(result)
return result
@threads()
def use_decorator(i):
time.sleep(i)
result = 'use_decorator: %s' % i
print(result)
return result
new_use_submit = Async(use_submit)
tasks = [new_use_submit(i) for i in (2, 1, 0)
] + [use_decorator(i) for i in (2, 1, 0)]
print([type(i) for i in tasks])
results = [i.x for i in tasks]
print(results)
# use_submit: 0
# use_decorator: 0
# [<class 'torequests.main.NewFuture'>, <class 'torequests.main.NewFuture'>, <class 'torequests.main.NewFuture'>, <class 'torequests.main.NewFuture'>, <class 'torequests.main.NewFuture'>, <class 'torequests.main.NewFuture'>]
# use_submit: 1
# use_decorator: 1
# use_submit: 2
# use_decorator: 2
# ['use_submit: 2', 'use_submit: 1', 'use_submit: 0', 'use_decorator: 2', 'use_decorator: 1', 'use_decorator: 0']
2. tPool - thread pool for async-requests
from torequests.main import tPool
from torequests.logs import print_info
trequests = tPool()
test_url = 'http://p.3.cn'
ss = [
trequests.get(
test_url,
retry=2,
callback=lambda x: (len(x.content), print_info(len(x.content))))
for i in range(3)
]
# or [i.x for i in ss]
trequests.x
ss = [i.cx for i in ss]
print_info(ss)
# [2018-03-18 21:18:09]: 612
# [2018-03-18 21:18:09]: 612
# [2018-03-18 21:18:09]: 612
# [2018-03-18 21:18:09]: [(612, None), (612, None), (612, None)]
3. Requests - aiohttp-wrapper
from torequests.dummy import Requests
from torequests.logs import print_info
trequests = Requests(frequencies={'p.3.cn': (2, 2)})
ss = [
trequests.get(
'http://p.3.cn', retry=1, timeout=5,
callback=lambda x: (len(x.content), print_info(trequests.frequencies)))
for i in range(4)
]
trequests.x
ss = [i.cx for i in ss]
print_info(ss)
# [2018-03-19 00:57:36]: {'p.3.cn': Frequency(sem=<1/2>, interval=2)}
# [2018-03-19 00:57:36]: {'p.3.cn': Frequency(sem=<0/2>, interval=2)}
# [2018-03-19 00:57:38]: {'p.3.cn': Frequency(sem=<1/2>, interval=2)}
# [2018-03-19 00:57:38]: {'p.3.cn': Frequency(sem=<2/2>, interval=2)}
# [2018-03-19 00:57:38]: [(612, None), (612, None), (612, None), (612, None)]
4. utils: some useful crawler toolkits
ClipboardWatcher: watch your clipboard changing.Counts: counter while every time being called.Null: will return self when be called, and alway be False.Regex: Regex Mapper for string -> regex -> object.Saver: simple object persistent toolkit with pickle/json.Timer: timing tool.UA: some common User-Agents for crawler.curlparse: translate curl-string into dict of request.md5: str(obj) -> md5_string.print_mem: show the proc-mem-cost with psutil, use this only for lazinesssss.ptime: %Y-%m-%d %H:%M:%S -> timestamp.ttime: timestamp -> %Y-%m-%d %H:%M:%Sslice_by_size: slice a sequence into chunks, return as a generation of chunks with size.slice_into_pieces: slice a sequence into n pieces, return a generation of n pieces.timeago: show the seconds as human-readable.unique: unique one sequence.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file torequests-4.8.6-py3-none-any.whl.
File metadata
- Download URL: torequests-4.8.6-py3-none-any.whl
- Upload date:
- Size: 45.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1f97260056a5f0c5c959f04ddea6fcaa18970974118f25a3cf4f94bd3a1f689a
|
|
| MD5 |
c14d8803d1c05da3505d893b390760e9
|
|
| BLAKE2b-256 |
9072193129d9ac614a9fdc59437261290dbd91b93225327339e2cc446a45b89b
|
File details
Details for the file torequests-4.8.6-py2-none-any.whl.
File metadata
- Download URL: torequests-4.8.6-py2-none-any.whl
- Upload date:
- Size: 47.0 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b01cac6e2d6ba7d28cdc66eb60f9e82dc467f1ca62795f64d224775dd810e76f
|
|
| MD5 |
758aa94b25ea6939d5c1f0b6bbf36076
|
|
| BLAKE2b-256 |
4b7c3b8a45e741259e6c4c1efabf6911fa236d0faf81880d684adcaca812910e
|