Dev/python
flask
lumination
2024. 4. 12. 14:14
WSGI Server (Gunicorn, uWSGI, mod_wsgi 또는 내장 werkzeug 벡자이크)
출처: etloveguitar.tistory/92
import random
import threading
import time
from werkzeug.local import LocalStack
thread_data_stack = LocalStack()
def long_running_function(thread_index):
thread_data_stack.push(f'index: {thread_index}, thread_id: {threading.get_native_id()}')
print(f'Starting thread #{thread_index}... {thread_data_stack}')
time.sleep(random.randrange(1, 11))
print(f'LocalStack contains: {thread_data_stack.top}')
print(f'Finished theread #{thread_index}!')
thread_data_stack.pop()
if __name__ == "__main__":
threads = []
for i in range(3):
thread = threading.Thread(target=long_running_function, args=(i,))
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
print('done')
Starting thread #0... <werkzeug.local.LocalStack object at 0x103393850>
Starting thread #1... <werkzeug.local.LocalStack object at 0x103393850>
Starting thread #2... <werkzeug.local.LocalStack object at 0x103393850>
LocalStack contains: index: 2, thread_id: 2676582
Finished theread #2!
LocalStack contains: index: 0, thread_id: 2676580
Finished theread #0!
LocalStack contains: index: 1, thread_id: 2676581
Finished theread #1!
done