hooliooo
10/26/2020, 3:21 PMval (firstImageBlobName, firstImageThumbnailBlobName, secondImageBlobName, secondImageThumbnailBlobName) = try {
coroutineScope {
AsyncImageUploads(
firstImageBlobName = async {
fileServiceInterface.uploadImage(
...
)
},
firstImageThumbnailBlobName = async {
fileServiceInterface.uploadImage(
...
)
},
secondImageBlobName = async {
fileServiceInterface.uploadImage(
...
)
},
secondImageThumbnailBlobName = async {
fileServiceInterface.uploadImage(
...
)
}
)
}
} catch (e: Exception) {
....
}
repository.save(
model.copy(
firstImage = firstImageRepository.save(
FirstImage(
imageURL = firstImageBlobName.await(),
thumbnailURL = firstImageThumbnailBlobName.await(),
model = model
)
),
secondImage = secondImageRepository.save(
SecondImage(
imageUrl = secondImageBlobName.await(),
thumbnailUrl = secondImageThumbnailBlobName.await(),
model = model
)
)
)
)
The issue is the upload takes around 40 seconds to finish 🤔 so I’m wondering if the async calls are happening concurrentlylouiscad
10/26/2020, 4:07 PMAsyncImageUploads
is implemented.hooliooo
10/27/2020, 7:18 AMmarstran
10/27/2020, 7:58 AMuploadImage
function suspending or blocking?Jonas Bark
10/27/2020, 8:17 AMhooliooo
10/27/2020, 8:19 AMuploadImage
is suspending. Under the hood the uploadImage function has image resizing with ImageIO and then uses awaitFirst/awaitFirstOrNull which according to the docs is non blocking.Jonas Bark
10/27/2020, 8:44 AMhooliooo
10/27/2020, 11:24 AM// coroutineScope
Current Thread for first-image-for-1: main @coroutine#5
4684 millis elapsed resizeImage first-image-for-1
Current Thread for first-image-thumb-for-1: main @coroutine#6
4828 millis elapsed resizeImage first-image-thumb-for-1
Current Thread for second-image-for-1: main @coroutine#7
4977 millis elapsed resizeImage second-image-for-1
Current Thread for second-image-thumb-for-1: main @coroutine#8
5033 millis elapsed resizeImage second-image-thumb-for-1
5075 millis elapsed uploadFile first-image-thumb-for-1
5080 millis elapsed uploadFile first-image-for-1
5086 millis elapsed uploadFile second-image-for-1
5097 millis elapsed uploadFile second-image-thumb-for-1
// withContext(<http://Dispatchers.IO|Dispatchers.IO>)
Current Thread for first-image-for-1: DefaultDispatcher-worker-3 @coroutine#5
Current Thread for second-image-for-1: DefaultDispatcher-worker-5 @coroutine#7
Current Thread for first-image-thumb-for-1: DefaultDispatcher-worker-2 @coroutine#6
Current Thread for second-image-thumb-for-1: DefaultDispatcher-worker-4 @coroutine#8
4775 millis elapsed resizeImage first-image-for-1
4788 millis elapsed resizeImage second-image-thumb-for-1
4802 millis elapsed resizeImage first-image-thumb-for-1
4837 millis elapsed resizeImage second-image-for-1
5060 millis elapsed uploadFile first-image-thumb-for-1
5068 millis elapsed uploadFile second-image-for-1
5074 millis elapsed uploadFile first-image-for-1
5084 millis elapsed uploadFile second-image-thumb-for-1
Seems like coroutineScope will execute the next coroutine after the resizeImage (blocking call)
while withContext(Dispatchers.IO), the coroutines are executed around the same time
Ignore the actual millis. I used an external start time (outside the function scope so the coroutines have the same start time reference)
The order in the uploadImage method is:
Current Thread for…
resizeImage
uploadFilemarstran
10/27/2020, 2:09 PMmain
thread before you changed the dispatcher.