0

I have a Python 2 django project, which was started with gunicorn, and write a lot of threading.currentThread().xxxxxx ='some value' in the code.

Because the coroutine reuses the same thread, I am curious how gevent guarantees that the currentThread variable created in coroutine A(Thread 1) will not affect coroutine B (same Thread 1).

After all, the writing on the code is:

import threading
threading.currentThread().xxxxx ='ABCD'

Instead of

import gevent
gevent.currentCoroutine().xxxxx ='ABCD' (simulate my guess)

thanks for your help

Tanen
  • 1

2 Answers2

0

It doesn't as far as I'm aware. Normal Gevent coroutines run in the same thread - if you modify something on that thread in one coroutine, it will be modified in the other coroutine as well.

If this is a question about gunicorn, that's a different matter and the following answer has some great detail on that - https://stackoverflow.com/a/41696500/7970018.

Wiggy A.
  • 496
  • 3
  • 16
0

You should create threading.localin mainThread. After monkey patching, Gevent patched gevent.local = threading.local so you can save data in current via:

import threading

threadlocal = threading.local()

def func_in_thread():
    # set data
    setattr(threadlocal, "_key", "_value")
    # do something
    # do something
    getattr(threadlocal, "_key", None)