gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
thread = threading.Thread(target=complete_captcha) As simple as possible, how to store…
- User
thread = threading.Thread(target=complete_captcha)
As simple as possible, how to store the returned value and store it in a list? as SIMPLE as possible, no explanation
- Assistant
import threading results = [] def complete_captcha(): result = "captcha_result" # Replace with actual logic results.append(result) thread = threading.Thread(target=complete_captcha) thread.start() thread.join() - User
dont i need to make it global?
- Assistant
Yes, you should declare the list as global inside the function if you are modifying it. Here’s the corrected code:
import threading results = [] def complete_captcha(): global results result = "captcha_result" # Replace with actual logic results.append(result) thread = threading.Thread(target=complete_captcha) thread.start() thread.join() - User
for _ in range(len(cookies)): thread = threading.Thread(target=login_attempt) thread.start() threads.append(thread)
- Assistant
To store the returned values from
login_attemptin a list while using threads, here's a simple way to do it:import threading results = [] threads = [] lock = threading.Lock() def login_attempt(): result = "login_result" # Replace with actual logic with lock: results.append(result) for _ in range(len(cookies)): thread = threading.Thread(target=login_attempt) thread.start() threads.append(thread) for thread in threads: thread.join()In this example, a lock is used to ensure that the
resultslist is updated safely by multiple threads.