Comments (11)
The .wait()
is added automatically by the @synchronized
decorator. When using @synchronized
it should usually never be necessary to worry about explicit synchronization.
from deco.
Why do I have to do it here then?
from deco.
.wait()
is called only when it's necessary, so whenever an argument or the result of a concurrent function is read or the @synchronized
function returns. For timing things like you're doing, you're using .wait()
totally correctly though. I should probably clarify this point in documentation somewhere.
As an alternative, you could probably time calls outside the @synchronized
function. This guarantees that .wait()
has been called and removes the need for it to be called explicitly.
from deco.
Oh. I think I get it. The @synchronized
function, upon completion, does the equivalent of doing .wait()
inside it.
from deco.
No, that's not it either. I'm sorry I'm so slow to get this but why doesn't this work without the .wait()
:
from deco import concurrent, synchronized
def slow():
time.sleep(5)
@concurrent
def download(url, data):
slow()
data[url] = url
@synchronized
def run(data):
for url in list('123456'):
download(url, data)
# download.wait()
somemute = {}
t0=time.time()
run(somemute)
t1=time.time()
pprint(somemute)
print "TOOK", t1-t0
The output of this is:
{}
TOOK 0.0250489711761
Why doesn't the @synchronized
decorator automatically do the .wait()
?
from deco.
Also, why 3 processors? Why not 4 or 11 or 47? Or why the number of cpus?
from deco.
That's actually a good catch, I'll need to look into this further but the AST parsing in v0.2 did not handle this case correctly. For whatever reason it now works in v0.3 and the latest version in github. I'll need to add an example/test that includes this, thanks for pointing it out!
The hard coded number of processors also got removed in the latest version.
Thanks for finding this!
from deco.
Thanks for the support. I'm so impressed by this that I decided to blog about it. This way it helps me understand it better. And remember it when I forget everything I've learned in 3 days.
https://www.peterbe.com/plog/deco
from deco.
Wow that's awesome! Do you mind if I link back to that in the readme somewhere? You've got a great set of examples that might help other people figure out how to use deco.
By the way, you can actually limit the number of process, @concurrent
passes it's arguments to multiprocessing.Pool
.
@concurrent(processes = 5)
def really_slow_and_intensive_thing():
...
Pool has some other interesting arguments you can look at here.
from deco.
Yes, please link to it.
I'll make an update about the ability to do processes=5
.
from deco.
Now I have a good follow-up too:
https://www.peterbe.com/plog/time-to-do-concurrent-cpu-bound-work
from deco.
Related Issues (20)
- Can deco decorates nested functions? HOT 1
- conc_test.py was eror in my python 3 HOT 1
- Processor limit? HOT 3
- Can I specify the number of processes to use manually? HOT 1
- Decorators from deco cannot be used on class methods HOT 3
- KeyError with peterbe.com easy example on WinPython 3.6.3.0-64 HOT 4
- Bug with deco function call HOT 2
- wiki problems HOT 1
- Issues With Hanging Processes / Restart HOT 2
- Is nbody.py a Deco example? HOT 1
- Specify fixed number of CPUs? HOT 1
- User problem: where to look for when passing and receiving complex data structures? HOT 2
- Is it a good idea to pass read-only variables in a global like manner to the @concurrent function? HOT 3
- "posonlyargs" missing from arguments HOT 3
- Code slows down without sleep HOT 7
- get() without synchronized returns a tuple? HOT 2
- Multiple for loops HOT 4
- TypeError: cannot pickle '_nrt_python._MemInfo' object when passing a numba Dict to the @concurrent function HOT 2
- error_callback possible for deco?
- Setting and handling timeout HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deco.