Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update timeout example in README.rst #134

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,28 @@ Pools support workers restart, timeout for long running tasks and more.

.. code:: python

from pebble import ProcessPool
from concurrent.futures import TimeoutError

TIMEOUT_SECONDS = 3

def function(foo, bar=0):
return foo + bar

def task_done(future):
try:
result = future.result() # blocks until results are ready
except TimeoutError as error:
print("Function took longer than %d seconds" % error.args[1])
except Exception as error:
print("Function raised %s" % error)
print(error.traceback) # traceback of the function

with ProcessPool(max_workers=5, max_tasks=10) as pool:
for index in range(0, 10):
future = pool.schedule(function, index, bar=1, timeout=TIMEOUT_SECONDS)
future.add_done_callback(task_done)
from pebble import ProcessPool
from concurrent.futures import TimeoutError

import time

TIMEOUT_SECONDS = 3

def function(foo, bar=0):
time.sleep(foo) # simulate a task
return foo
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bar is not used anymore, the original README was returning foo + bar.


def task_done(future):
try:
result = future.result() # blocks until results are ready
print("Process %d returned result" % result)
except TimeoutError as error:
print("Process took longer than %d seconds" % error.args[1])
except Exception as error:
print("Process raised %s" % error)
print(error.traceback) # traceback of the function

with ProcessPool(max_workers=5, max_tasks=10) as pool:
for index in range(0, 10):
future = pool.schedule(function, [index], {'bar':1}, timeout=TIMEOUT_SECONDS)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use KW arguments properly to better indicate how to call this function.

future = pool.schedule(function, args=[index], kwargs={'bar':1}, timeout=TIMEOUT_SECONDS)

future.add_done_callback(task_done)