# How to run HTTP Post request as background task?

**URL:** <https://discourse.julialang.org/t/how-to-run-http-post-request-as-background-task/49673>\
**Category:** Performance\
**Tags:** question, multithreading\
**Created:** [November 6, 2020, 9:18am UTC](https://discourse.julialang.org/t/how-to-run-http-post-request-as-background-task/49673 "2020-11-06T09:18:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![uwechsler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uwechsler/32/5223_2.png) [@uwechsler](https://discourse.julialang.org/u/uwechsler)\
**Post date:** [November 6, 2020, 9:18am UTC](https://discourse.julialang.org/t/how-to-run-http-post-request-as-background-task/49673/1 "2020-11-06T09:18:17Z")

</div>

Hi everybody,

I am working on a wrapper for the `Seq` logger for `Julia` (see [WIP project](https://github.com/ueliwechsler/SeqLoggers.jl))  
Using the [REST API for posting raw events](https://docs.datalust.co/docs/posting-raw-events) , it is possible to post log events to the `Seq` logging server.

My current implementation sends a POST request after every log event (i.e. `@debug`, `@info`, `@warn`, or `@error`).

The request takes some time to finish, therefoe, I’d like to run these requests as a background task.

Currently, I use the following approach

```julia
Threads.@spawn begin
     HTTP.request("POST", url, header, event)
end

```

which spawns a new task without waiting for the result. Performance-wise this works quite well, but it is hacky since I don’t wait for a potential error message if the POST request fails.

Is it possible to run this post request as a proper background task without blocking the execution of the other code but still receiving the return value of the request?

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [November 9, 2020, 6:16pm UTC](https://discourse.julialang.org/t/how-to-run-http-post-request-as-background-task/49673/2 "2020-11-09T18:16:25Z")

</div>

Create a pool of threads that post to the server. These post workers should read the post information from a channel. They can write the response if it is a failure to another channel which is read by a response worker task that handles the failure. The original code now writes the post information to the channel. Adjust channel and pool sizes so that nothing blocks.

---

<div class="post-metadata">

**Author:** ![uwechsler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uwechsler/32/5223_2.png) [@uwechsler](https://discourse.julialang.org/u/uwechsler)\
**Post date:** [November 10, 2020, 9:56pm UTC](https://discourse.julialang.org/t/how-to-run-http-post-request-as-background-task/49673/3 "2020-11-10T21:56:34Z")

</div>

Thanks for your reply! Do you have any example code on how to create that thread pool and how to read and write to the channels?

Another solution I found is the package [WorkerUtilities](https://github.com/JuliaServices/WorkerUtilities.jl) where you could call it like this, I guess:

```julia
worker = WorkerUtilities.@spawn begin
     HTTP.request("POST", url, header, event)
end
response = fetch(worker)

```

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [November 11, 2020, 2:59am UTC](https://discourse.julialang.org/t/how-to-run-http-post-request-as-background-task/49673/4 "2020-11-11T02:59:15Z")

</div>

The manual has examples here [Asynchronous Programming · The Julia Language](https://docs.julialang.org/en/v1.5/manual/asynchronous-programming/). Since your HTTP.Post is not cpu bound, your workers can probably be async tasks rather than threads.
