madisonmay / tomorrow Goto Github PK
View Code? Open in Web Editor NEWMagic decorator syntax for asynchronous code in Python
License: MIT License
Magic decorator syntax for asynchronous code in Python
License: MIT License
Current blocker is function serialization. A potential solution might use dill
and override __getstate__
and __setstate__
.
You said
Magic decorator syntax for asynchronous code in Python 2.7+
✔ ~/projects[master ↓·2|…2]
10:25 $ python3 get_urls.py
Traceback (most recent call last):
File "get_urls.py", line 4, in
from tomorrow import threads
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tomorrow/init.py", line 1, in
from tomorrow import threads
ImportError: cannot import name 'threads'
✔ ~/projects[master ↓·2|…2]
10:25 $ python get_urls.py
Time: 4.356341 seconds
In the second example where you added Tomorrow to the first example, you had a second import of time after the ifmain. Is this necessary?
I didn't see any explanation of why it was added.
such as @threads(50,timeout=1), will easily raise an error, so how to retry the func then?
thanks for answering.
urls = ['http://p.3.cn/prices/mgets?skuIds=J_1273600'] * 1000
import time
import requests
# from multiprocessing.dummy import Pool
from tomorrow import threads
@threads(50,timeout=0.1)
def download(url):
return requests.get(url)
if __name__ == "__main__":
start = time.time()
responses = [download(url) for url in urls]
html = [len(response.text) for response in responses]
print(html)
end = time.time()
print("Time: %f seconds" % (end - start))
some codes
Error
Traceback (most recent call last):
File "D:\project\Tomorrow\tests\test.py", line 95, in test_future_function
assert true()
TypeError: 'Tomorrow' object is not callable
<tomorrow.tomorrow.Tomorrow object at 0x029885D0>
<tomorrow.tomorrow.Tomorrow object at 0x02988730>
Failure
Traceback (most recent call last):
File "D:\project\Tomorrow\tests\test.py", line 68, in test_shared_executor
assert (N * DELAY) < (end - start) < (2 * N * DELAY)
AssertionError
<tomorrow.tomorrow.Tomorrow object at 0x02988810>
<tomorrow.tomorrow.Tomorrow object at 0x02988930>
Should probably end up in README.md.
My sense is that this will work in Python 3.2 and later but not in Python 2. Is that correct? It would be useful if that was clearly stated in both the readme and in the pypi record.
The tomorrow module runs a "Syntax Error" in Python version 3.8.1 due to the name "async". I made a correction by changing the function name to "_async".
Change def async(n, base_type, timeout=None):
to
def _async(n, base_type, timeout=None):
And def threads(n, timeout=None):
change return return async(n, ThreadPoolExecutor, timeout)
to
return _async(n, ThreadPoolExecutor, timeout)
I have no idea how to solve this one, but when trying to return strings from a function decorated with an @threads, rather than executing, it simply errors. Code below to reproduce:
def download_nm(delimiter, source, dump):
images = [line.split(delimiter)[10] for line in open(source)][1:] # avoid header row
with open(dump, 'a') as sink:
for i, image in enumerate(images):
results = return_image_json(image) # type(results) == <type 'instance'>
sink.write(results) # Error, expected string or buffer
@threads(16)
def return_image_json(image_link):
response = requests.get(image_link)
encoded = "data:%s;base64,%s" % (response.headers['Content-Type'], base64.b64encode(response.content))
return json.dumps({image_link: encoded}) + '\n'
It looks like moving to new-style classes resolves this issue, but since you're relying on some of the syntax-hacks of old-style classes I'm not sure if this is solvable.
Hello,
After doing pip install tomorrow
- you will find that your tests directory is installed along with the tomorrow package into your site-packages.
Here are complete contents of site-packages/tomorrow-0.2.3-py3.4.egg-info/installed-files.txt
file on my ubuntu box:
../tests/test.py
../tests/__init__.py
../tomorrow/__init__.py
../tomorrow/tomorrow.py
../tests/__pycache__/test.cpython-34.pyc
../tests/__pycache__/__init__.cpython-34.pyc
../tomorrow/__pycache__/__init__.cpython-34.pyc
../tomorrow/__pycache__/tomorrow.cpython-34.pyc
./
SOURCES.txt
dependency_links.txt
top_level.txt
requires.txt
PKG-INFO
Most packages do not distribute their own unit tests. The ones that do - have a uniquely named package, so that it would never clash with anything a user project might have.
This is how I found out about the bug, I was trying to import tests.fixtures
in my own project, where file tests/fixtures.py
exists and I kept getting an ImportError
.
Please consider adding exclude
argument in find_packages
call in your setup.py
file. Thanks.
Hi madison,
I am very excited of your python module. But can you change the code so that I can use threads() as decorator but not set an n. So the number of threads append dynamicly to the threadpool?
Thank you very much.
The function with the decoration @threads returns an object of the class Tomorrow even when I return an integer or some other type from the function.
Sample code
from tomorrow import threads
@threads(10)
def test(i):
return i
for i in range(10):
print(test(i))
Output
<tomorrow.tomorrow.Tomorrow object at 0x022AA030>
<tomorrow.tomorrow.Tomorrow object at 0x022AA030>
<tomorrow.tomorrow.Tomorrow object at 0x022AA030>
<tomorrow.tomorrow.Tomorrow object at 0x0228FAD0>
<tomorrow.tomorrow.Tomorrow object at 0x01F44B90>
<tomorrow.tomorrow.Tomorrow object at 0x022AA030>
<tomorrow.tomorrow.Tomorrow object at 0x0228FAD0>
<tomorrow.tomorrow.Tomorrow object at 0x01F44B90>
<tomorrow.tomorrow.Tomorrow object at 0x022AA030>
<tomorrow.tomorrow.Tomorrow object at 0x0228FAD0>
When tomorrow is used the python script won't end. Ctrl+C won't stop it, only closing the process alltogether will stop it. Needless to say, this is pretty frustrating.
import time
import requests
from tomorrow import threads
urls=[
'http://www.baidu.com',
'http://www.sina.com',
'http://www.ifeng.com',
'http://www.13393.com',
'http://www.bing.com'
]
@threads(5)
def dowload(url):
return requests.get(url)
if name == 'main':
start=time.time()
responses=[dowload(url) for url in urls]
html=[response.text for response in responses]
end=time.time()
print 'Time:%f seconds' %(end-start)
Traceback (most recent call last):
File "/home/kaka/workspace/PythonProject/Https/PC/tomorrowdemo.py", line 5, in
from tomorrow import threads
File "/home/kaka/workspace/PythonProject/Https/PC/tomorrow.py", line 4, in
ImportError: cannot import name threads
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.