GithubHelp home page GithubHelp logo

Comments (4)

mkizesov avatar mkizesov commented on June 16, 2024

AsyncFHIRClient make asynchronous I/O calls, that means those calls are non-blocking. And of course it's ready to use it with any python asyncio features.
If I understand correctly, you're trying to run these tasks concurrently.
So the answer to your question is to use asyncio.gather.
I'll provide you with the basic example based on your code. It works and it's a good starting point, but it's not the best way to do it. For example, if any task throws an exception, you'll not be able to get any results in the patients list.

import asyncio
from fhirpy import AsyncFHIRClient
from aiohttp import BasicAuth


async def main():
    # Create an instance
    client = AsyncFHIRClient(
        'http://localhost:8080/',
        authorization=BasicAuth('user', 'password').encode()
    )

    pat_ids = [
        "patient1",
        "patient2",
        "patient3",
        "patient4",
        "patient5",
    ]

    resources = client.resources('Patient')

    # Create a list of coroutines (not awaited)
    tasks = [
        resources.search(_id=pat_id).first()
        for pat_id in pat_ids
    ]

    patients = await asyncio.gather(*tasks)
    print(patients)


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

Also, there's no need to use client.schema = None and fhir_version='4.0.0', because we've removed schema validation since version 1.0.0.

from fhir-py.

kylejbrk avatar kylejbrk commented on June 16, 2024

@mkizesov
That makes sense. It looks like this only work with .first()? I wouldn't be able to retrieve all the results from a search? Never mind. Just had to replace with .fetch()

One of the things I'd like to do is get all the encounters for many patient id's.

Also, from the code you posted it looks like I can authenticate with a username and password as well a Bearer Token? That's pretty neat if so.

from fhir-py.

mkizesov avatar mkizesov commented on June 16, 2024

The value of "authorization" argument is used in the Authorization header of each lib request. If your FHIR-server supports basic auth, BasicAuth('user', 'password').encode() will do its job. If your server use OAuth 2 (for example), you can use any 3rd party library to authenticate on the server and get Bearer token. Then provide this token to AsyncFHIRClient's "authorization" argument.

Yep, any fhirpy coroutine function (count, fetch, get, first, fetch_all, etc.) could be used there.

Another example suitable to speed up some independent tasks like retrieving and updating patient.

import asyncio
from fhirpy import AsyncFHIRClient
from aiohttp import BasicAuth


client = AsyncFHIRClient(
    'http://localhost:8080/',
    authorization=BasicAuth('root', 'secret').encode()
)
patient_resources = client.resources('Patient')


async def update_patient(patient_id):
    patient = await patient_resources \
        .search(_id=patient_id).first()
    if patient:
        # change patient here

        await patient.save()
        print(f'Patient "{patient_id}" was updated')
    else:
        print(f'Patient "{patient_id}" wasn\'t found')


async def main():
    pat_ids = [
        "patient1",
        "patient2",
        "patient3",
        "patient4",
        "patient5",
    ]

    resources = client.resources('Patient')

    # Create a list of coroutines (not awaited)
    tasks = [
        update_patient(pat_id) for pat_id in pat_ids
    ]

    await asyncio.gather(*tasks)


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

from fhir-py.

kylejbrk avatar kylejbrk commented on June 16, 2024

Oh thanks again. I could use something like the above to write all the results of the individual tasks to disk.

Our implementation uses OAuth2 so I've been using the bearer token method. Wasn't sure if the other would work as well.

Very helpful thank you.

from fhir-py.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.