tech:python_-_callback
Différences
Ci-dessous, les différences entre deux révisions de la page.
| tech:python_-_callback [2025/03/24 15:06] – créée - modification externe 127.0.0.1 | tech:python_-_callback [2026/05/30 21:31] (Version actuelle) – Jean-Baptiste | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | < | ||
| + | {{tag> | ||
| + | |||
| + | # Python - callback | ||
| + | |||
| + | Voir : | ||
| + | * [[Notes Python multithreading]] | ||
| + | |||
| + | ## Introduction | ||
| + | |||
| + | Exemple d'une fonction sans callback | ||
| + | ~~~python | ||
| + | #! / | ||
| + | |||
| + | from functools import reduce | ||
| + | |||
| + | |||
| + | def calcfact(i): | ||
| + | assert i >= 0 | ||
| + | return reduce(lambda a, b: a * b, range(1, i+1) if i > 1 else [1]) | ||
| + | |||
| + | |||
| + | print(calcfact(3)) | ||
| + | ~~~ | ||
| + | |||
| + | Exemple de la même fonction avec callback optionnel | ||
| + | ~~~python | ||
| + | #! / | ||
| + | |||
| + | from functools import reduce | ||
| + | |||
| + | |||
| + | def calcfact_called(i, | ||
| + | print(i, '! = ', res, sep='' | ||
| + | |||
| + | |||
| + | def calcfact(i, callback=None): | ||
| + | assert i >= 0 | ||
| + | res = reduce(lambda a, b: a * b, range(1, i+1) if i > 1 else [1]) | ||
| + | if callback is None: | ||
| + | return res | ||
| + | else: | ||
| + | callback(i, res) | ||
| + | |||
| + | # Sans Callback | ||
| + | print(calcfact(3)) | ||
| + | |||
| + | # Avec Callback | ||
| + | calcfact(4, calcfact_called) | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | ## Exemple avec async / asyncio / await | ||
| + | |||
| + | ~~~python | ||
| + | #! / | ||
| + | |||
| + | import asyncio | ||
| + | |||
| + | |||
| + | def waitplop_called(i): | ||
| + | print(i) | ||
| + | |||
| + | |||
| + | async def waitplop(i, callback=None): | ||
| + | await asyncio.sleep(i) | ||
| + | if callback is None: | ||
| + | return i | ||
| + | else: | ||
| + | callback(i) | ||
| + | |||
| + | |||
| + | async def main(): | ||
| + | async with asyncio.TaskGroup() as tg: | ||
| + | # Asynchronous with callback | ||
| + | tg.create_task(waitplop(3, | ||
| + | tg.create_task(waitplop(3, | ||
| + | |||
| + | # Synchronous / sequential without callback | ||
| + | task1 = await tg.create_task(waitplop(4)) | ||
| + | print(task1) | ||
| + | |||
| + | |||
| + | asyncio.run(main()) | ||
| + | ~~~ | ||
| + | |||
| + | ~~~ | ||
| + | $ time ./ | ||
| + | 3 | ||
| + | 3 | ||
| + | 4 | ||
| + | |||
| + | real 0m4.120s | ||
| + | user 0m0.095s | ||
| + | sys | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ### Autre façon de faire un callback avec asyncio | ||
| + | |||
| + | Voir : | ||
| + | * https:// | ||
| + | |||
| + | ~~~python | ||
| + | #! / | ||
| + | |||
| + | import asyncio | ||
| + | |||
| + | |||
| + | def waitplop_called(task): | ||
| + | print(task.result()) | ||
| + | |||
| + | |||
| + | async def waitplop(i): | ||
| + | await asyncio.sleep(i) | ||
| + | return i | ||
| + | |||
| + | |||
| + | async def main(): | ||
| + | task = asyncio.create_task(waitplop(3)) | ||
| + | | ||
| + | await asyncio.gather(task) | ||
| + | |||
| + | |||
| + | asyncio.run(main()) | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Autres | ||
| + | |||
| + | Voir : | ||
| + | * https:// | ||
| + | |||
| + | ~~~python | ||
| + | # Call ' | ||
| + | fut.add_done_callback( | ||
| + | functools.partial(print, | ||
| + | ~~~ | ||
| + | |||
