背景
后端接收到一個用戶請求后俩由,在請求其他第三方接口時妆档,需要攜帶用戶請求中的trace_id痹愚,以便后續(xù)梳理請求鏈路和定位問題。
原有的代碼基于Python的Django框架開發(fā)器赞,不同的用戶請求之間是線程隔離的,不同用戶請求的trace_id自然也不同墓拜。除此之外港柜,為了便于讀取trace_id所以將其存儲為一個全局變量】劝瘢基于這兩點夏醉,原代碼中采用threading.local()
來存儲trace_id。
為了加快請求速度贿衍,需要通過多線程來并發(fā)請求第三方接口(PS:也可以使用協(xié)程授舟,但是原有的接口調(diào)用封裝并不支持異步,改動起來工作量太大)贸辈。但是由于threading.local()
本就是線程隔離的释树,所以子線程根本無法拿到父線程(請求線程)的trace_id,然后就會出現(xiàn)報錯:'_thread._local' object has no attribute 'trace_id'
擎淤。
在簡單的搜索以后奢啥,發(fā)現(xiàn)Java中自帶InheritableThreadLocal
(與ThreadLocal
不同,使用InheritableThreadLocal
創(chuàng)建的變量的屬性可以被子線程繼承嘴拢,以繼續(xù)使用)桩盲。但是不清楚為什么Python沒有(PS:事實上stackoverflow上面也有提問,但是不知道為什么沒有回答)席吴,所以做了一個簡單的實現(xiàn)赌结。
代碼實現(xiàn)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import copy
from _threading_local import local
from weakref import ref
from threading import current_thread, Thread
inheritable_local_list = []
def copy_dict(local_obj, child_thread):
"""copy parent(current) thread local_obj dict for the child thread, and return it."""
impl = local_obj._local__impl
localdict = copy.deepcopy(impl.dicts[id(current_thread())][1])
key = impl.key
idt = id(child_thread)
def local_deleted(_, key=key):
# When the localimpl is deleted, remove the thread attribute.
thread = wrthread()
if thread is not None:
del thread.__dict__[key]
def thread_deleted(_, idt=idt):
# When the thread is deleted, remove the local dict.
# Note that this is suboptimal if the thread object gets
# caught in a reference loop. We would like to be called
# as soon as the OS-level thread ends instead.
local = wrlocal()
if local is not None:
dct = local.dicts.pop(idt)
wrlocal = ref(impl, local_deleted)
wrthread = ref(child_thread, thread_deleted)
child_thread.__dict__[key] = wrlocal
impl.dicts[idt] = wrthread, localdict
return localdict
class InheritableThread(Thread):
def __init__(self, *args, **kwargs):
Thread.__init__(self, *args, **kwargs)
global inheritable_local_list
for local_obj in inheritable_local_list:
copy_dict(local_obj, self)
class InheritableLocal(local):
"""
Please use InheritableThread to create threads, if you want your son threads can inherit parent threads' local
"""
def __init__(self):
global inheritable_local_list
inheritable_local_list.append(self)
threading.local()
的結(jié)構(gòu):file
使用方法
thread_ctx = InheritableLocal()
t = InheritableThread(target=function, args=(arg1, arg2, arg3))
t.start()
t.join()
- 使用
InheritableLocal()
替代threading.local()
來創(chuàng)建一個線程環(huán)境變量,沒有其他副作用 - 使用
InheritableThread()
替代Thread()
來創(chuàng)建一個線程孝冒,同樣沒有副作用
然后創(chuàng)建出來的子線程就可以繼承父線程的環(huán)境變量了柬姚,當然兩個線程之間的環(huán)境變量還是相互隔離的,繼承只會發(fā)生在子線程創(chuàng)建時庄涡。