|
| 1 | +# Example: Parallel Processing and Batch Context |
| 2 | + |
| 3 | +When dealing with a vast amount of documents, or extracting information continuously in a backend server context, using `emulate_async` prevents UI blocking and increases thoroughput. |
| 4 | + |
| 5 | +## 1. Extracting Invoices Using Async Tasks |
| 6 | + |
| 7 | +This example demonstrates how to use `emulate_async` alongside `dataclasses` and `asyncio.gather` for highly concurrent validation. We define a data structure for invoice sender coordinates, and extract it instantly from multiple unstructured snippets. |
| 8 | + |
| 9 | +```python |
| 10 | +import asyncio |
| 11 | +from dataclasses import dataclass |
| 12 | +from OpenHosta import emulate_async |
| 13 | + |
| 14 | +@dataclass |
| 15 | +class InvoiceSender: |
| 16 | + company_name: str |
| 17 | + address: str |
| 18 | + city: str |
| 19 | + postal_code: str |
| 20 | + siret_number: str |
| 21 | + |
| 22 | +async def extract_sender_coordinates(invoice_text: str) -> InvoiceSender: |
| 23 | + """ |
| 24 | + Parses the invoice text to find the exact coordinates of the invoice sender. |
| 25 | + Does not extract the recipient! |
| 26 | + """ |
| 27 | + return await emulate_async() |
| 28 | + |
| 29 | +async def main(): |
| 30 | + invoice_1 = "From: ACME Corp Ltd. 12 rue de la Paix, Paris, 75000. SIRET: 123456789. Billed to: John Doe." |
| 31 | + invoice_2 = "Facture envoyée le 12 Mars. Expéditeur: BricoPro. 5 impasse des artisans, 69002 Lyon. N° SIRET : 987654321." |
| 32 | + |
| 33 | + # Using asyncio.create_task or asyncio.gather allows both requests to be sent |
| 34 | + # to the API / Local model concurrently! |
| 35 | + tasks = [ |
| 36 | + asyncio.create_task(extract_sender_coordinates(invoice_1)), |
| 37 | + asyncio.create_task(extract_sender_coordinates(invoice_2)) |
| 38 | + ] |
| 39 | + |
| 40 | + results = await asyncio.gather(*tasks) |
| 41 | + |
| 42 | + for res in results: |
| 43 | + print(f"Company: {res.company_name} | City: {res.city} | SIRET: {res.siret_number}") |
| 44 | + |
| 45 | +# Run the event loop |
| 46 | +if __name__ == "__main__": |
| 47 | + asyncio.run(main()) |
| 48 | +``` |
| 49 | + |
| 50 | +## 2. The BatchDataContext Manager (Coming soon) |
| 51 | + |
| 52 | +OpenHosta provides an elegant context manager under development `BatchDataContext` designed exclusively to simplify multi-processing workloads without manually tampering with event loops or explicit tasks. |
| 53 | + |
| 54 | +```python |
| 55 | +from OpenHosta import emulate_async |
| 56 | +from OpenHosta import BatchDataContext |
| 57 | + |
| 58 | +async def name_list(topic: str) -> list[str]: |
| 59 | + """Generates three names related to the topic.""" |
| 60 | + return await emulate_async() |
| 61 | + |
| 62 | +async def first_name(person: str) -> str: |
| 63 | + """Returns the first name of the famous person.""" |
| 64 | + return await emulate_async() |
| 65 | + |
| 66 | +async def alt_name(person: str) -> str: |
| 67 | + """Returns an alternative alias of the person.""" |
| 68 | + return await emulate_async() |
| 69 | + |
| 70 | +# Batch size allows processing N queries in a sliding window |
| 71 | +with BatchDataContext(batch_size=10) as my_data: |
| 72 | + # Case A: Function inherently returning a list |
| 73 | + my_data["A"] = name_list("Macron") |
| 74 | + |
| 75 | + # Case B: Python Array encapsulating multiple separate Async Coroutines |
| 76 | + my_data["B"] = [first_name("Trump"), alt_name("Trump")] |
| 77 | + |
| 78 | + # Case C: Inserting strict generic static data |
| 79 | + my_data["C"] = "Static Data" |
| 80 | + |
| 81 | +# Block closes, wait occurs, then dictionary items are completely resolved! |
| 82 | +print("Résultat final :", my_data) |
| 83 | +``` |
0 commit comments