Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d6a4086

Browse files
Create thread_specific_data_2.py
1 parent 795921f commit d6a4086

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

‎scripts/thread_specific_data_2.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import random
2+
import threading
3+
import logging
4+
5+
logging.basicConfig(level=logging.DEBUG,
6+
format='(%(threadName)-10s) %(message)s',
7+
)
8+
9+
10+
def show_value(data):
11+
try:
12+
val = data.value
13+
except AttributeError:
14+
logging.debug('No value yet')
15+
else:
16+
logging.debug('value=%s', val)
17+
18+
def worker(data):
19+
show_value(data)
20+
data.value = random.randint(1, 100)
21+
show_value(data)
22+
23+
class MyLocal(threading.local):
24+
def __init__(self, value):
25+
logging.debug('Initializing %r', self)
26+
self.value = value
27+
28+
local_data = MyLocal(1000)
29+
show_value(local_data)
30+
31+
for i in range(2):
32+
t = threading.Thread(target=worker, args=(local_data,))
33+
t.start()
34+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /