Concurrent API requests

Hello

I am investigating about how to make concurrent API calls using the Python SDK, but I am encountering some problems.

Here is my testing python script. Its goal is to print the usernames of a list of users given their ids:

# Imports

# Authentication stuff

user_ids = # List of ids

usernames = queue.Queue()

# Callback function
put_username_in_queue = lambda user : usernames.put(user.username)


usersApi = PureCloudPlatformClientV2.UsersApi()
threads = [usersApi.get_user(id, callback=put_username_in_queue) for id in user_ids]

for thread in threads:
    thread.join()

while not usernames.empty():
    print(usernames.get())

However, in the console this message is printed: WARNING Connection pool is full, discarding connection: api.mypurecloud.ie

Also, not all the usernames are printed (e.g. for a list of 400 ids, it prints only 383 usernames)

Could someone explain to me how to do this concurrent calls, please?

Thank you

The connection pool defaults to 4. If you need to change that, you can create your own instance of RESTClientObject and provide the desired pool size in the constructor. You can set your instance of RestClientObject on ApiClient via ApiClient.rest_client. The default instance is set in ApiClient's constructor.

Hello

I have changed the RESTClientObject pool size to 50, 100, 500 and 1000, but in all cases it keeps showing that warning messages (and their frequency are not reduced)

The threading is a feature of python's internal libraries, so if you've set the pool size and are using the object correctly, it should work. I've created issue API-5046 to investigate python's thread pool behavior.

I have modified my python script to limit the requests simultaneously made to 5 too, using a semaphore (while keeping the pool size at 1000), but it still shows the WARNING message. Here is the code:

# Imports
import json
import queue
import threading

import PureCloudPlatformClientV2
from PureCloudPlatformClientV2.rest import RESTClientObject

# Client config
PureCloudPlatformClientV2.configuration.host = "https://api.mypurecloud.ie"
apiclient = PureCloudPlatformClientV2.api_client.ApiClient()
apiclient.rest_client = RESTClientObject(1000)
client_id = "my_client_id"
client_secret = "my_client_secret"
apiclient = apiclient.get_client_credentials_token(client_id, client_secret)
PureCloudPlatformClientV2.configuration.api_client = apiclient
usersApi = PureCloudPlatformClientV2.UsersApi()

# User ids import
with open('ids.json') as ids_file:
    user_ids = json.load(ids_file)

usernames = queue.Queue()
semaphore = threading.Semaphore(5)
threads = []

# Callback function
def put_username_in_queue(user):
    usernames.put(user.username)
    semaphore.release()

# Requests
while user_ids:
    semaphore.acquire()
    try:
        user_id = user_ids.pop(0)
        threads.append(usersApi.get_user(user_id, callback=put_username_in_queue))
    except:
        print('An exception has occurred!!')
        semaphore.release()

# Wait until complete
[t.join() for t in threads]

# Printing
while not usernames.empty():
    print(usernames.get())

Hello

Have you discovered something about this behaviour?

Thanks

No, the issue is currently on the backlog.

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.

This has been resolved as of 79.2.0. You can now set max_size in the constructor of RESTClientObject, which will control the maximum number of concurrent request threads used.