I also came across a similar problem when executing several POST instructions using the python request library in Spark. To make matters worse, I used multiprocessing for each artist to send to the server. Thus, thousands of connections created in seconds took several seconds to change state from TIME_WAIT and free up ports for the next set of connections.
Of all the available solutions available on the Internet that talk about disabling keep-alive using with request.Session () and others, I found this answer to work. You may need to put the contents of the header on the separte line outside the command line.
headers = { 'Connection': 'close' } with requests.Session() as session: response = session.post('https://xx.xxx.xxx.x/xxxxxx/x', headers=headers, files=files, verify=False) results = response.json() print results
source share