Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Multi threading and processing eye-candy.

License

NotificationsYou must be signed in to change notification settings

noxdafox/pebble

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pebble provides a neat API to manage threads and processes within an application.

Source:https://github.com/noxdafox/pebble
Documentation:https://pebble.readthedocs.io
Download:https://pypi.org/project/Pebble/

Build StatusDocumentation StatusPyPI - Downloads

Examples

Run a job in a separate thread and wait for its results.

frompebbleimportconcurrent@concurrent.threaddeffunction(foo,bar=0):returnfoo+barfuture=function(1,bar=2)result=future.result()# blocks until results are ready

Same code with AsyncIO support.

importasynciofrompebbleimportasynchronous@asynchronous.threaddeffunction(foo,bar=0):returnfoo+barasyncdefasynchronous_function():result=awaitfunction(1,bar=2)# blocks until results are readyprint(result)asyncio.run(asynchronous_function())

Run a function with a timeout of ten seconds and deal with errors.

frompebbleimportconcurrentfromconcurrent.futuresimportTimeoutError@concurrent.process(timeout=10)deffunction(foo,bar=0):returnfoo+barfuture=function(1,bar=2)try:result=future.result()# blocks until results are readyexceptTimeoutErroraserror:print("Function took longer than %d seconds"%error.args[1])exceptExceptionaserror:print("Function raised %s"%error)print(error.traceback)# traceback of the function

Pools support workers restart, timeout for long running tasks and more.

frompebbleimportProcessPoolfromconcurrent.futuresimportTimeoutErrorTIMEOUT_SECONDS=3deffunction(foo,bar=0):returnfoo+bardeftask_done(future):try:result=future.result()# blocks until results are readyexceptTimeoutErroraserror:print("Function took longer than %d seconds"%error.args[1])exceptExceptionaserror:print("Function raised %s"%error)print(error.traceback)# traceback of the functionwithProcessPool(max_workers=5,max_tasks=10)aspool:forindexinrange(0,10):future=pool.schedule(function,index,bar=1,timeout=TIMEOUT_SECONDS)future.add_done_callback(task_done)

[8]ページ先頭

©2009-2025 Movatter.jp