-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from pgorecki/async
Add async support. Closes #2
- Loading branch information
Showing
15 changed files
with
703 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import asyncio | ||
|
||
from lato import Application, TransactionContext | ||
|
||
|
||
async def add_async(a, b): | ||
await asyncio.sleep(1) | ||
return a + b | ||
|
||
|
||
if __name__ == "__main__": | ||
with TransactionContext() as ctx: | ||
result = ctx.call(add_async, 1, 2) | ||
print(result) | ||
|
||
result = asyncio.run(result) | ||
|
||
print("got result from asyncio.run", result) | ||
|
||
app = Application("async") | ||
result = app.call(add_async, 1, 2) | ||
print(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import asyncio | ||
|
||
from lato import Application, Command | ||
|
||
|
||
class MultiplyCommand(Command): | ||
a: int | ||
b: int | ||
|
||
|
||
app = Application("async") | ||
|
||
|
||
@app.handler(MultiplyCommand) | ||
async def multiply_async(command: MultiplyCommand): | ||
await asyncio.sleep(1) | ||
return command.a * command.b | ||
|
||
|
||
coroutine = app.execute(MultiplyCommand(a=10, b=20)) | ||
print("execution result", coroutine) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import asyncio | ||
import logging | ||
|
||
from lato import Application, TransactionContext | ||
|
||
logging.basicConfig(level=logging.DEBUG) | ||
root_logger = logging.getLogger("toy") | ||
|
||
app = Application() | ||
|
||
|
||
class Counter: | ||
def __init__(self): | ||
self.value = 0 | ||
|
||
def next(self): | ||
self.value += 1 | ||
return self.value | ||
|
||
|
||
counter = Counter() | ||
|
||
|
||
@app.on_enter_transaction_context | ||
async def on_enter_transaction_context(ctx: TransactionContext): | ||
correlation_id = str(counter.next()) | ||
logger = root_logger.getChild(correlation_id) | ||
ctx.set_dependencies(logger=logger) | ||
logger.info("Connecting to database") | ||
await asyncio.sleep(0.001) | ||
logger.info("Connected") | ||
|
||
|
||
@app.on_exit_transaction_context | ||
async def on_exit_transaction_context(ctx: TransactionContext, exception=None): | ||
logger = ctx["logger"] | ||
logger.info("Disconnecting from database") | ||
await asyncio.sleep(0.001) | ||
logger.info("Disconnected from database") | ||
|
||
|
||
@app.handler("foo") | ||
async def handle_foo(x, logger): | ||
logger.info(f"Starting foo, x={x}") | ||
await asyncio.sleep(0.001) | ||
logger.info("Finished foo") | ||
|
||
|
||
async def main() -> None: | ||
await asyncio.gather( | ||
app.call_async("foo", x=1), | ||
app.call_async("foo", x=2), | ||
app.call_async("foo", x=3), | ||
) | ||
|
||
|
||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.