What is so bad with threadlocals

前端 未结 5 673
感动是毒
感动是毒 2021-01-01 12:40

Everybody in Django world seems to hate threadlocals(http://code.djangoproject.com/ticket/4280, http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser). I read Armin

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-01 12:45

    You want to use threadlocals when you're working with multiple threads and want to localize some objects to a specific thread, eg. having one database connection for each thread. In your case, you want to use it more as a global context (if I understand you correctly), which is probably a bad idea. It will make your app a bit slower, more coupled and harder to test.

    Why is passing it from request not worth it? Why don't you store it in session or user profile?

    There difference with Java is that web development there is much more stateful than in Python/PERL/PHP/Ruby world so people are used to all kind of contexts and stuff like that. I don't think that is an advantage, but it does seem like it at the beginning.

提交回复
热议问题