Giter Site home page Giter Site logo

Comments (3)

noxdafox avatar noxdafox commented on June 16, 2024 1

Goal with pebble is not to be concurrent.futures compatible. Only in recent versions I adopted a somewhat similar API to make it more friendly and also to re-use the Future class.

I find the Executor API pretty simplistic and I don't think that adding a custom namespace _pebble_specific_timeout is the correct solution.

I will reinstate the schedule API as the recommended function for submitting jobs to the Pools and leave the submit for mere compatibility with asyncio.loop.run_in_executor.

Main point here is that there are 2 sets of parameters/arguments:

  1. The ones which are Pool specific (what to run and how)
  2. The ones that are function specific (the function args and kwargs)

The two should be logically separated and not intertwined.

from pebble.

noxdafox avatar noxdafox commented on June 16, 2024

Hello,

the pool.submit has been designed to be compatible with concurrent.future.Executor.submit for the sake of supporting asyncio.loop.run_in_executor API.

The concurrent.future.Executor.submit API passes arguments and keyword arguments straight to the submitted function making positional arguments the only way to provide submission specific parameters.

This is evident in the function signature Executor.submit(fn, /, *args, **kwargs) note the / marking positional only parameters.

It would not be possible, in fact, to provide a signature such as ProcessPool.submit(fn, *args, timeout=None, **kwargs) as this would prevent submitting functions with a timeout keyword arguments such as requests.get. Moreover, the above API would not allow to submit jobs within asyncio.loop.run_in_executor as it would not be possible to submit them altogether with a timeout.

If you want to use ThreadPool and ProcessPool interchangeably and you don't care about the timeout functionality then you can simply use a class wrapper for it.

class Pool:
    def __init__(pool: [pebble.ProcessPool, pebble.ThreadPool]):
        self._pool = pool

    def submit(fn, *args, **kwargs):
         if isinstance(self._pool, pebble.ProcessPool):
            self._pool.submit(fn, None, *args, **kwargs)

pool = Pool(pebble.ProcessPool())
futures = [pool.submit(func, i) for i in range(10)]

Keep in mind that ThreadPool and ProcessPool do not provide the same behaviour. Futures returned by ProcessPool can be cancelled and can timeout. In such cases, the underlying process will be interrupted avoiding to consume extra resources. The same is not true for ThreadPool due to the fact that running threads cannot be interrupted. Hence, you need to keep this aspect in mind when exchanging one pool with the other.

from pebble.

victoryhb avatar victoryhb commented on June 16, 2024

Thanks a lot for the enlightening remarks on the rationale behind such a design decision.
I see that the positional timeout argument was added mainly to be compatible with libs like asyncio.
Coming from concurrent.futures, however, I was surprised by the subtle incompatibity in the API. I personallly would have preferred full compatibility by adding a special keyword argument like pebble_timeout, which also avoids the somewhat redundant use of None in many cases.

from pebble.

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.