Created
April 17, 2023 23:39
-
-
Save emre/01e8acac22562a64b7f3c0967c33093f to your computer and use it in GitHub Desktop.
openai lib race condition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Author
emre
commented
Apr 17, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment