Skip to content

Instantly share code, notes, and snippets.

@emre
Created April 17, 2023 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emre/01e8acac22562a64b7f3c0967c33093f to your computer and use it in GitHub Desktop.
Save emre/01e8acac22562a64b7f3c0967c33093f to your computer and use it in GitHub Desktop.
openai lib race condition
import openai
from threading import Thread
import time
def work_with_openai(tid):
key = f"key_{tid}"
openai.api_key = key
print(f"Thread {tid} set api key as {key}")
time.sleep(0.1)
print(f"Thread {tid} sends a request with {openai.api_key}")
def main():
threads = []
for i in range(5):
t = Thread(
target=work_with_openai,
args=(i+1,))
t.start()
threads.append(t)
for t in threads:
t.join()
if __name__ == '__main__':
main()
@emre
Copy link
Author

emre commented Apr 17, 2023

Thread 1 set api key as key_1
Thread 2 set api key as key_2
Thread 3 set api key as key_3
Thread 4 set api key as key_4
Thread 5 set api key as key_5

Thread 2 sends a request with key_5
Thread 3 sends a request with key_5
Thread 4 sends a request with key_5
Thread 1 sends a request with key_5
Thread 5 sends a request with key_5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment