Comments (4)
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.
@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.
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.
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)
- Don't pass _count if _id is specified HOT 1
- Getting _elements when using reference not supported HOT 2
- Is there an API to handle the ValueSet resource? HOT 2
- Delete via reference currently requires download first HOT 2
- Persistent session support HOT 1
- Issue with client.execute() Method Removing Parameters from Path HOT 1
- Unable to use items() on AttrDict HOT 2
- Add support for `_cascade=delete` param to `delete` method HOT 1
- Why the save() function will cost a lot of time for saving one Resource?
- Fix resource fetching for /fhir base urls
- TypeError: Cannot serialize non-str key None HOT 1
- Next URL - relative to hostname HOT 3
- Example re: paging through a FHIR bundle? HOT 1
- AsyncFHIRClient's aiohttp throwing SSLCertVerificationError HOT 4
- Saving Device with reference to Patient not successful HOT 5
- Hashable resources HOT 10
- Add support for chained search params without specifying resource type
- Create aiohttp.ClientSession once per application rather than per request HOT 2
- Add fetch_raw_all support HOT 1
- Handle references HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fhir-py.