Giter Club home page Giter Club logo

Comments (11)

alex-sherman avatar alex-sherman commented on May 29, 2024

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.

peterbe avatar peterbe commented on May 29, 2024

Why do I have to do it here then?

from deco.

alex-sherman avatar alex-sherman commented on May 29, 2024

.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.

peterbe avatar peterbe commented on May 29, 2024

Oh. I think I get it. The @synchronized function, upon completion, does the equivalent of doing .wait() inside it.

from deco.

peterbe avatar peterbe commented on May 29, 2024

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.

peterbe avatar peterbe commented on May 29, 2024

Also, why 3 processors? Why not 4 or 11 or 47? Or why the number of cpus?

from deco.

alex-sherman avatar alex-sherman commented on May 29, 2024

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.

peterbe avatar peterbe commented on May 29, 2024

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.

alex-sherman avatar alex-sherman commented on May 29, 2024

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.

peterbe avatar peterbe commented on May 29, 2024

Yes, please link to it.
I'll make an update about the ability to do processes=5.

from deco.

peterbe avatar peterbe commented on May 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.