Sending POST Requests without waiting for response?
You cannot just send data without receiving an answer with HTTP. HTTP always goes request -> response. Even if the response is just very short (like a simple 200 with no text), there needs to be a response. And every HTTP socket will wait for that response.
If you don't care about the response, you could add a process to the server that makes your requests, and you just push your request data to it (like a service that is running in the background, checking a request database, and always starting the request whenever a new entry was added). That way you would make the request asynchronously and could quit as soon as you added that request to the stack.
Also as meouw said, the client is not part of any communication you are doing with php. Php is a server-side language, so when the client requests a webpage (the php file), the server executes that file (and does all requests the php file states) and then returns the result to the client.
How to work with request.post() method without waiting for response
If you are not interested in response from the server(fire and forget) then you can use an async library for this. But I must warn you, you cannot mix sync and async code(actually you can but it's not worth dealing with it) so most of your codes must be changed.
Another approach is using threads, they can call the url
seperately wait for the response without affecting rest of your code.
Something like this will help:
def request_task(url, json, headers):
requests.post(url, json=data, headers=headers)
def fire_and_forget(url, json, headers):
threading.Thread(target=request_task, args=(url, json, headers)).start()
...
fire_and_forget(url, json=data, headers=headers)
Brief info about threads
Threads are seperate flow of execution. Multiple threads run concurrently so when a thread is started it runs seperately from current execution. After starting a thread, your program just continue to execute next instructions while the instructions of thread also executes concurrently. For more info, I recommend realpython introduction to threads.
Threaded HTTP post via requests without waiting for request to go through
You could use threads and simply dont wait for them to finish:
from threading import Thread
import time
def request():
# value_to_send = some_function()
# x = requests.post(url, json = myjson)
print('started')
time.sleep(.5)
print("request done!")
def main():
while True:
t = Thread(target=request)
t.start()
time.sleep(.25)
main()
Output:
started
started
request done!
started
request done!
started
request done!
...
How can I send a GET request without waiting for the response
Here is a way to run GET
asynchronously and in a intra-session non-blocking manner (observer returning nothing):
library(shiny)
library(future)
library(promises)
library(future.callr)
library(httr)
plan(callr)
queryGoogle <- function(queryString) {
myResponse <- httr::GET("http://google.com/", path = "search", query = list(q = queryString))
return(myResponse)
}
ui <- fluidPage(
br(),
textOutput("time_output"),
br(),
textInput(inputId="query_input", label = NULL, value = "", placeholder = "Search google..."),
actionButton("import", "Query"),
hr(),
textOutput("query_output")
)
server <- function(input, output, session) {
futureData <- reactiveValues(response = NULL)
observeEvent(input$import, {
myFuture <- future({
queryGoogle(isolate(input$query_input))
})
then(
myFuture,
onFulfilled = function(value) {
futureData$response <- value
},
onRejected = NULL
)
return(NULL)
})
output$query_output <- renderPrint({
req(futureData$response)
})
time <- reactive({
invalidateLater(500, session)
Sys.time()
})
output$time_output <- renderText({ paste("Something running in parallel:", time()) })
}
shinyApp(ui, server)
This is a slight modification of my answer here.
Please also read Joe Cheng's related answer here carefully.
Related Topics
Query With Multiple Values in a Column
Create Line Break in Whatsapp Message
How to Change the Background Color of Dropdown List in Select Tag
Send Python Output to HTML Field
Install PHP Intl Extension on Macos
Php - Mysqli_Fetch_Array() Expects Parameter 1 to Be Mysqli_Result, String Given
How to Get Data from Another Table Using Id from Another Table
Php Substring Remove Everything in a String After Certain Character
How to Generate Unique Random Value for Each User in Laravel and Add It to Database
Option Selected With Codeigniter and Data from Db
Laravel, Failed to Authenticate on Smtp Server With Username "" Using 3 Possible Authenticators
Tell Composer to Use Different PHP Version
How to Get Username from Facebook Id
How to Get Foreach Loop Variable Outside of Loop
The Get Method Is Not Supported for This Route. Supported Methods: Put. But I'M Using Put
Laravel - Display a Pdf File in Storage Without Forcing Download