This file is indexed.

/usr/share/doc/python-myghty/html/scopedpython.html is in python-myghty 1.1-6.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
<html>
<head>
	<title>
Myghty Documentation
</title>
	<link href="style.css" rel="stylesheet" type="text/css"></link>
	<link href="syntaxhighlight.css" rel="stylesheet" type="text/css"></link>
</head>
<body>




<link href="docs.css" rel="stylesheet" type="text/css"></link>


<div style="position:absolute;left:0px;top:0px;"><a name="top"></a>&nbsp;</div>

<div class="doccontainer">

<div class="docheader">

<div class="docheadertext" >Myghty Documentation</div>
<div class="">Version: 1.1   Last Updated: 09/10/06 19:32:29</div>
</div>



		<A name="scopedpython"></a>
		
	
<div class="topnav">


	
	<div class="topnavcontrol">
	View: <b>Paged</b> &nbsp;|&nbsp; <a href="documentation.html">One Page</a>
	</div>



<div class="topnavsectionlink">

<a href="index.html">Table of Contents</a>

<div class="prevnext">
Previous: <a href="embedding.html">Embedding Python in HTML</a>

&nbsp; | &nbsp;


Next: <a href="globals.html">Standard Global Variables</a>

</div>
</div>


<div class="topnavmain">
	<div class="topnavheader">Scoped <%python> Blocks</div>
	<div class="topnavitems">
	


    
    
    <div class="toclink">
        <A style="" href="#scopedpython_component">Component Scope</a>
    </div>
    
    <div class="toclinkcontainer">
    </div>


    
    
    <div class="toclink">
        <A style="" href="#scopedpython_global">Global Scope</a>
    </div>
    
    <div class="toclinkcontainer">
    </div>


    
    
    <div class="toclink">
        <A style="" href="#scopedpython_init">Init Scope</a>
    </div>
    
    <div class="toclinkcontainer">
    </div>


    
    
    <div class="toclink">
        <A style="" href="#scopedpython_cleanup">Cleanup Scope</a>
    </div>
    
    <div class="toclinkcontainer">
    </div>


    
    
    <div class="toclink">
        <A style="" href="#scopedpython_request">Request Scope</a>
    </div>
    
    <div class="toclinkcontainer">
        
        
    <div class="toclinkcontainer">
    
    <div class="smalltoclink">
    <A href="#scopedpython_request_value">Using Pass-By-Value</a>
    </div>

    </div>


        
        
    <div class="toclinkcontainer">
    
    <div class="smalltoclink">
    <A href="#scopedpython_request_alternative">Alternative - Request Attributes</a>
    </div>

    </div>


    </div>


    
    
    <div class="toclink">
        <A style="" href="#scopedpython_thread">Thread Scope</a>
    </div>
    
    <div class="toclinkcontainer">
        
        
    <div class="toclinkcontainer">
    
    <div class="smalltoclink">
    <A href="#scopedpython_thread_threadlocal">Alternative to Thread Scope: ThreadLocal()</a>
    </div>

    </div>


    </div>



	</div>
</div>

</div>

		<div class="sectioncontent">
			




<div class="subsection" style="margin-left:10px;">

    <div class="sectiontext">


	<p>The <%python> tag is also capable of specifying Python code that occurs within a specific scope of a template's execution, using the <span class="codeline">scope</span> attribute.   Scopes are provided that specify Python code as always the first thing executed within a component, always the last thing executed, or executes once per request, once per application thread, or once per application process.</p>
	
	<p>Originally, these scopes had their own specific tag names, which are still available.  These names include all those that are familiar to HTML::Mason users, as well as some new names.  All synonyms are noted in each section.  </p>

	
			
	
	
			



	
			


	
			


	
			



	
			



    </div>

        



<A name="scopedpython_component"></a>

<div class="subsection" style="margin-left:20px;">

    <span class="sectionheadertext">Component Scope</span>
    <div class="sectiontext">


	<p><b>Usage:</b>  <%python scope="component"></p>
	<p><b>Also called:</b>  <%python></p>
	<p>A component-scoped block is a regular block of Python that executes within the body of a template, as illustrated in the previous section <a href="embedding.html#embedding_blocks" >Python Blocks</a> .  Many component-scoped blocks may exist in a template and they will all be executed in the place that they occur.  Component scope has two variants, init and cleanup scope, which while still being component scope, execute at specific times in a component's execution regardless of their position in a template.  They are described later in this section.</p>
	
	
    </div>


    <a href="#scopedpython" class="toclink">back to section top</a>


</div>


        



<A name="scopedpython_global"></a>

<div class="subsection" style="margin-left:20px;">

    <span class="sectionheadertext">Global Scope</span>
    <div class="sectiontext">


	<p><b>Usage:</b>  <%python scope="global"></p>
	<p><b>Also called:</b>  <%global>, <%once> </p>
	<p>A global-scoped block refers to a section of Python code that is executed exactly once for a component, within the scope of its process.  In reality, this usually means code that is executed each time the module is newly loaded or reloaded in the current Python process, as it is called as part of the module import process.  Variables that are declared in a global-scoped block are compiled as global variables at the top of the generated module, and thus can be seen from within the bodies of all components defined within the module.  Assignment to these variables follows the Python scoping rules, meaning that they will magically be copied into local variables once assigned to unless they are pre-declared in the local block with the Python keyword <span class="codeline">global</span>.</p>
	
	<p>Global-scoped Python executes only once when a component module is first loaded, as part of its import.  As such, it is called slightly outside of the normal request call-chain and does not have the usual access to the the built-in request-scoped variables <i>m</i>, <i>r</i>, ARGS, etc.  However, you can access the Request that is corresponding to this very first execution via the static call <span class="codeline">request.instance()</span>, which will give you the current request instance that is servicing the global-scoped block. </p>

	

<div class="code">
<pre><span class="text"> 
 </span><span class="blocktag">&lt;%python scope="global"&gt;</span>
     <span class="python_comment"># declare global variables, accessible</span>
     <span class="python_comment"># across this component's generated module</span>
     
<span class="python_operator">     </span><span class="python_name">message1 </span><span class="python_operator">= </span><span class="python_literal">"this is message one."</span><span class="python_operator">
     </span><span class="python_name">message2 </span><span class="python_operator">= </span><span class="python_literal">"this is message two."</span><span class="python_operator">
     </span><span class="python_name">message3 </span><span class="python_operator">= </span><span class="python_literal">"doh, im message three."</span><span class="python_operator">
</span> <span class="blocktag">&lt;/%python&gt;</span><span class="text">
 
 </span><span class="blocktag">&lt;%python&gt;</span>
     <span class="python_comment"># reference the global variables</span>
<span class="python_operator">     </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">write</span><span class="python_enclosure">(</span><span class="python_literal">"message one: " </span><span class="python_operator">+ </span><span class="python_name">message1</span><span class="python_enclosure">)</span><span class="python_operator">
     </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">write</span><span class="python_enclosure">(</span><span class="python_literal">"message two: " </span><span class="python_operator">+ </span><span class="python_name">message2</span><span class="python_enclosure">)</span><span class="python_operator">
     </span>
     <span class="python_comment"># we want to assign to message3,</span>
     <span class="python_comment"># so declare "global" first</span>
     <span class="python_keyword">global </span><span class="python_name">message3</span><span class="python_operator">
     </span>
     <span class="python_name">message3 </span><span class="python_operator">= </span><span class="python_literal">"this is message three."</span><span class="python_operator">
     </span>
     <span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">write</span><span class="python_enclosure">(</span><span class="python_literal">"message three: " </span><span class="python_operator">+ </span><span class="python_name">message3</span><span class="python_enclosure">)</span><span class="python_operator">
     </span>
<span class="python_operator"></span> <span class="blocktag">&lt;/%python&gt;</span></pre></div>

	
	<p>Use a global-scoped Python block to declare constants and resources which are shareable amongst all components within a template file.  Note that for a threaded environment, global-scoped section applies to all threads within a process.  This may not be appropriate for certain kinds of non-threadsafe resources, such as database handles, certain dbm modules, etc.  For thread-local global variable declaration, see the section <a href="scopedpython.html#scopedpython_thread" >Thread Scope</a>, or use the Myghty <span class="codeline">ThreadLocal()</span> object described in <a href="scopedpython.html#scopedpython_thread_threadlocal" >Alternative to Thread Scope: ThreadLocal()</a>.  Similarly, request-scoped operation is provided as well, described in the section <a href="scopedpython.html#scopedpython_request" >Request Scope</a>.</p>
	
	<p>A global-scoped block can only be placed in a top-level component, i.e. cannot be used within %def or %method.</p>
	
    </div>


    <a href="#scopedpython" class="toclink">back to section top</a>


</div>


        



<A name="scopedpython_init"></a>

<div class="subsection" style="margin-left:20px;">

    <span class="sectionheadertext">Init Scope</span>
    <div class="sectiontext">



	<p><b>Usage:</b> <%python scope="init">
	<p><b>Also called:</b> <%init></p>

	<p>Init-scoped Python is python code that is executed once per component execution, before any other local python code or text is processed.  It really is just a variant of component scope, since it is still technically within component scope, just executed before any other component-scoped code executes.    One handy thing about init scope is that you can stick it at the <i>bottom</i> of a big HTML file, or in any other weird place, and it will still execute before all the other local code.  Its recommended as the place for setting up HTTP headers which can only be set before any content and/or whitespace is written (although if autoflush is disabled, this is less of an issue; see <a href="filtering.html#filtering_autoflush" >The Autoflush Option</a> for more details).</p>
	
	<p>In this example, a login function is queried to detect if the current browser is a logged in user.  If not, the component wants to redirect to a login page.  Since a redirect should occur before any output is generated, the login function and redirect occurs within an init-scoped Python block:</p>
	
	

<div class="code">
<pre><span class="blocktag">&lt;%python scope="init"&gt;</span>
    <span class="python_comment"># check that the user is logged in, else</span>
    <span class="python_comment"># redirect them to a login page</span>
<span class="python_operator">    </span><span class="python_keyword">if not </span><span class="python_name">user_logged_in</span><span class="python_enclosure">()</span><span class="python_operator">:
        </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">send_redirect</span><span class="python_enclosure">(</span><span class="python_literal">"/login.myt"</span><span class="python_operator">, </span><span class="python_name">hard </span><span class="python_operator">= </span><span class="python_name">True</span><span class="python_enclosure">)</span><span class="python_operator">
</span><span class="blocktag">&lt;/%python&gt;</span><span class="text">  

</span><span class="doctag">&lt;%doc&gt;</span><span class="doctag_text">rest of page follows....</span><span class="doctag">&lt;/%doc&gt;</span><span class="text">   
&lt;html&gt;
    &lt;head&gt;
    ....</span></pre></div>

	
	
    </div>


    <a href="#scopedpython" class="toclink">back to section top</a>


</div>


        



<A name="scopedpython_cleanup"></a>

<div class="subsection" style="margin-left:20px;">

    <span class="sectionheadertext">Cleanup Scope</span>
    <div class="sectiontext">


	<p><b>Usage:</b> <%python scope="cleanup">
	<p><b>Also called:</b> <%cleanup></p>

	<p>Cleanup-scoped Python is Python code executed at the end of everything else within a component's execution.  It is executed within the scope of a <span class="codeline">try..finally</span> construct so its guaranteed to execute even in the case of an error condition.</p>
	
	<p>In this example, a hypothetical LDAP database is accessed to get user information.  Since the database connection is opened within the scope of the component inside its init-scoped block, it is closed within the cleanup-scoped block:</p>

	

<div class="code">
<pre><span class="argstag">&lt;%args&gt;</span>
<span class="python_operator">    </span><span class="python_name">userid</span><span class="python_operator">
</span><span class="argstag">&lt;/%args&gt;</span><span class="text">
</span><span class="blocktag">&lt;%python scope="init"&gt;</span>
    <span class="python_comment"># open a connection to an expensive resource</span>
<span class="python_operator">    </span><span class="python_name">ldap </span><span class="python_operator">= </span><span class="python_name">Ldap</span><span class="python_operator">.</span><span class="python_name">connect</span><span class="python_enclosure">()</span><span class="python_operator">
    </span><span class="python_name">userrec </span><span class="python_operator">= </span><span class="python_name">ldap</span><span class="python_operator">.</span><span class="python_name">lookup</span><span class="python_enclosure">(</span><span class="python_name">userid</span><span class="python_enclosure">)</span><span class="python_operator">
</span><span class="blocktag">&lt;/%python&gt;</span><span class="text">
    
    name: </span><span class="substitution">&lt;%</span><span class="python_operator"> </span><span class="python_name">userrec</span><span class="python_enclosure">[</span><span class="python_literal">'name'</span><span class="python_enclosure">]</span><span class="python_operator"></span><span class="substitution">%&gt;</span><span class="text">&lt;br/&gt;
    address: </span><span class="substitution">&lt;%</span><span class="python_operator"> </span><span class="python_name">userrec</span><span class="python_enclosure">[</span><span class="python_literal">'address'</span><span class="python_enclosure">]</span><span class="python_operator"></span><span class="substitution">%&gt;</span><span class="text">&lt;br/&gt;
    email: </span><span class="substitution">&lt;%</span><span class="python_operator"> </span><span class="python_name">userrec</span><span class="python_enclosure">[</span><span class="python_literal">'email'</span><span class="python_enclosure">]</span><span class="python_operator"></span><span class="substitution">%&gt;</span><span class="text">&lt;br/&gt;
    
</span><span class="blocktag">&lt;%python scope="cleanup"&gt;</span>
    <span class="python_comment"># insure the expensive resource is closed</span>
<span class="python_operator">    </span><span class="python_keyword">if </span><span class="python_name">ldap </span><span class="python_keyword">is not </span><span class="python_name">None</span><span class="python_operator">:
        </span><span class="python_name">ldap</span><span class="python_operator">.</span><span class="python_name">close</span><span class="python_enclosure">()</span><span class="python_operator">
</span><span class="blocktag">&lt;/%python&gt;</span></pre></div>

	
	
    </div>


    <a href="#scopedpython" class="toclink">back to section top</a>


</div>


        



<A name="scopedpython_request"></a>

<div class="subsection" style="margin-left:20px;">

    <span class="sectionheadertext">Request Scope</span>
    <div class="sectiontext">



	<p><b>Usage:</b> <%python scope="request">
	<p><b>Also called:</b> <%requestlocal>, <%requestonce> or <%shared></p>
	
	<p>A request-scoped Python block has similarities to a global-scoped block, except instead of executing at the top of the generated component's module, it is executed within the context of a function definition that is executed once per request.  Within this function definition, all the rest of the component's functions and variable namespaces are declared, so that when variables, functions and objects declared within this function are referenced, they are effectively unique to the individual request.</p>

	

<div class="code">
<pre><span class="blocktag">&lt;%python scope="request"&gt;</span>
<span class="python_operator">    </span><span class="python_name">context </span><span class="python_operator">= </span><span class="python_enclosure">{}</span><span class="python_operator">
</span><span class="blocktag">&lt;/%python&gt;</span><span class="text">

</span><span class="controlline">%</span><span class="python_operator"> </span><span class="python_name">context</span><span class="python_enclosure">[</span><span class="python_literal">'start'</span><span class="python_enclosure">] </span><span class="python_operator">= </span><span class="python_name">True</span><span class="python_operator"></span>
<span class="compcall">&lt;&amp; </span><span class="compname">dosomething </span><span class="compcall">&amp;&gt;</span><span class="text">

</span><span class="deftag">&lt;%def</span><span class="compname"> dosomething</span><span class="deftag">&gt;</span><span class="text">
</span><span class="controlline">%</span><span class="python_operator"> </span><span class="python_keyword">if </span><span class="python_name">context</span><span class="python_operator">.</span><span class="python_name">has_key</span><span class="python_enclosure">(</span><span class="python_literal">'start'</span><span class="python_enclosure">)</span><span class="python_operator">:</span>
<span class="text">    hi
</span><span class="controlline">%</span><span class="python_operator"> </span><span class="python_keyword">else</span><span class="python_operator">:</span>
<span class="text">    bye
</span><span class="deftag">&lt;/%def&gt;</span></pre></div>

	
	
	<p>The good news is, the regular Myghty variables <i>m</i>, <i>ARGS</i>, <i>r</i> etc. are all available within a request-scoped block.  Although since a request-scoped block executes within a unique place in the call-chain, the full functionality of <i>m</i>, such as component calling, is not currently supported within such a block.</p>

	<p>Request-scoped sections can only be used in top-level components, i.e. cannot be used within %def or %method.</p>	

	
			

	
			
 	
	
    </div>

        



<A name="scopedpython_request_value"></a>

<div class="subsection" style="margin-left:30px;">

    <span class="sectionheadertext">Using Pass-By-Value</span>
    <div class="sectiontext">


	
	<p>While the net result of a request-scoped Python block looks similar to a global-scoped block, there are differences with how declared variables are referenced.  Since they are not module-level variables, they can't be used with the Python <span class="codeline">global</span> keyword.  There actually is no way to directly change what a request-scoped variable points to; however this can be easily worked around through the use of pass-by-value variables.  A pass-by-value effect can be achieved with an array, a dictionary, a user-defined object whose value can change, or the automatically imported Myghty datatype <span class="codeline">Value()</span>, which represents an object whose contents you can change:</p>
	
	

<div class="code">
<pre><span class="blocktag">&lt;%python scope="request"&gt;</span>
<span class="python_operator">    </span><span class="python_name">status </span><span class="python_operator">= </span><span class="python_name">Value</span><span class="python_enclosure">(</span><span class="python_literal">"initial status"</span><span class="python_enclosure">)</span><span class="python_operator">
</span><span class="blocktag">&lt;/%python&gt;</span><span class="text">

</span><span class="blocktag">&lt;%python&gt;</span>
<span class="python_operator">    </span><span class="python_keyword">if </span><span class="python_name">some_status_changed</span><span class="python_enclosure">()</span><span class="python_operator">:
        </span><span class="python_name">status</span><span class="python_operator">.</span><span class="python_name">assign</span><span class="python_enclosure">(</span><span class="python_literal">"new status"</span><span class="python_enclosure">)</span><span class="python_operator">
</span><span class="blocktag">&lt;/%python&gt;</span><span class="text">

the status is </span><span class="substitution">&lt;%</span><span class="python_operator"> </span><span class="python_name">status</span><span class="python_enclosure">()</span><span class="python_operator"></span><span class="substitution">%&gt;</span></pre></div>

	
    </div>


    <a href="#scopedpython" class="toclink">back to section top</a>


</div>


        



<A name="scopedpython_request_alternative"></a>

<div class="subsection" style="margin-left:30px;">

    <span class="sectionheadertext">Alternative - Request Attributes</span>
    <div class="sectiontext">


	
	<p>An alternative to using request-scoped blocks is to assign attributes to the request object, which can then be accessed by any other component within that request.  This is achieved via the member <a href="request.html#request_members_attributes" >attributes</a>:</p>
	

<div class="code">
<pre><span class="blocktag">&lt;%python&gt;</span>
<span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">attributes</span><span class="python_enclosure">[</span><span class="python_literal">'whatmawsaw'</span><span class="python_enclosure">] </span><span class="python_operator">= </span><span class="python_literal">'i just saw a flyin turkey!'</span><span class="python_operator">
</span><span class="blocktag">&lt;/%python&gt;</span><span class="text">

</span><span class="doctag"># .... somewhere in some other part of the template
</span><span class="text">

JIMMY: Hey maw, what'd ya see ?  
MAW: </span><span class="substitution">&lt;%</span><span class="python_operator"> </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">attributes</span><span class="python_enclosure">[</span><span class="python_literal">'whatmawsaw'</span><span class="python_enclosure">]</span><span class="python_operator"></span><span class="substitution">%&gt;</span></pre></div>

	<p>Produces:</p>
	

<div class="code">
<pre><span class="text">JIMMY: Hey maw, what'd ya see ?
MAW: i just saw a flyin turkey!</span></pre></div>

 	
 	<p>Also see <a href="request.html#request" >The Request</a> which lists all request methods, including those used for attributes.
 	</p>
 	
    </div>


    <a href="#scopedpython" class="toclink">back to section top</a>


</div>





</div>


        



<A name="scopedpython_thread"></a>

<div class="subsection" style="margin-left:20px;">

    <span class="sectionheadertext">Thread Scope</span>
    <div class="sectiontext">


	
	<p><b>Usage:</b>  <%python scope="thread"></p>
	<p><b>Also called:</b> <%threadlocal>, <%threadonce></p>
	
	<p>A thread-scoped Python block is nearly the same as a <a href="scopedpython.html#scopedpython_request" >Request Scope</a> block, except its defining function is executed once per thread of execution, rather than once per request.  In a non-threaded environment, this amounts to once per process.  The standard global variables <i>m</i>, <i>r</i> etc. are still available, but their usefulness is limited, as they only represent the one particular request that happens to be the first request to execute within the current thread.  Also, like request-scope, variables declared in a thread-scoped block cannot be changed except with pass-by-value techniques, described in <a href="scopedpython.html#scopedpython_request_value" >Using Pass-By-Value</a></p>
	
	<p>In this example, a <span class="codeline">gdbm</span> file-based database is accessed to retreive weather information keyed off of a zipcode.  Since gdbm uses the 
	<span class="codeline">flock()</span> system call, it's a good idea to keep the reference to a gdbm handle local to a particular thread (at least, the thread that opens the database must be the one that closes it).  The reference to gdbm is created and initialized within a thread-scoped block to insure this behavior:</p>
	
	

<div class="code">
<pre><span class="blocktag">&lt;%python scope="thread"&gt;</span>
    <span class="python_comment"># use GNU dbm, which definitely doesnt work in </span>
    <span class="python_comment"># multiple threads (unless you specify 'u')</span>
<span class="python_operator">    </span><span class="python_keyword">import </span><span class="python_name">gdbm</span><span class="python_operator">
    </span><span class="python_name">db </span><span class="python_operator">= </span><span class="python_name">gdbm</span><span class="python_operator">.</span><span class="python_name">open</span><span class="python_enclosure">(</span><span class="python_literal">"weather.dbm"</span><span class="python_operator">, </span><span class="python_literal">'r'</span><span class="python_enclosure">)</span><span class="python_operator">
</span><span class="blocktag">&lt;/%python&gt;</span><span class="text">

</span><span class="argstag">&lt;%args&gt;</span>
<span class="python_operator">    </span><span class="python_name">zipcode</span><span class="python_operator">
</span><span class="argstag">&lt;/%args&gt;</span><span class="text">

temperature in your zip for today is: </span><span class="substitution">&lt;%</span><span class="python_operator"> </span><span class="python_name">db</span><span class="python_enclosure">[</span><span class="python_name">zipcode</span><span class="python_enclosure">]</span><span class="python_operator"></span><span class="substitution">%&gt;</span></pre></div>

	<p>Use a thread-scoped block to declare global resources which are not thread-safe.  A big candidate for this is a database connection, or an object that contains a database-connection reference, for applications that are not trying too hard to separate their presentation code from their business logic (which, of course, they should be).  </p>

	<p>Thread-scoped sections can only be used in top-level components, i.e. cannot be used within %def or %method.</p>	

	
			
	
	
    </div>

        



<A name="scopedpython_thread_threadlocal"></a>

<div class="subsection" style="margin-left:30px;">

    <span class="sectionheadertext">Alternative to Thread Scope: ThreadLocal()</span>
    <div class="sectiontext">



	<p>A possibly higher-performing alternative to a thread-scoped section is to declare thread-local variables via the automatically imported class <span class="codeline">ThreadLocal()</span>.  The <span class="codeline">ThreadLocal()</span> class works similarly to a <span class="codeline">Value()</span> object, except that assigning and retrieving its value attaches the data internally to a dictionary, keyed off of an identifier for the current thread.  In this way each value can only be accessed by the thread which assigned the value, and other threads are left to assign their own value:</p>

	

<div class="code">
<pre><span class="blocktag">&lt;%python scope="global"&gt;</span>
<span class="python_operator">    </span><span class="python_name">x </span><span class="python_operator">= </span><span class="python_name">ThreadLocal</span><span class="python_enclosure">()</span><span class="python_operator">
</span><span class="blocktag">&lt;/%python&gt;</span><span class="text">

</span><span class="blocktag">&lt;%python&gt;</span>
<span class="python_operator">    </span><span class="python_keyword">import </span><span class="python_name">time</span><span class="python_operator">
    </span><span class="python_name">x</span><span class="python_operator">.</span><span class="python_name">assign</span><span class="python_enclosure">(</span><span class="python_literal">"the current time is " </span><span class="python_operator">+ </span><span class="python_name">repr</span><span class="python_enclosure">(</span><span class="python_name">time</span><span class="python_operator">.</span><span class="python_name">time</span><span class="python_enclosure">()))</span><span class="python_operator">
</span><span class="blocktag">&lt;/%python&gt;</span><span class="text">

value of x: </span><span class="substitution">&lt;%</span><span class="python_operator"> </span><span class="python_name">x</span><span class="python_enclosure">()</span><span class="python_operator"></span><span class="substitution">%&gt;</span></pre></div>


	<p><span class="codeline">ThreadLocal()</span> also supports automatic creation of its value per thread, by supplying a pointer to a function to the parameter <span class="codeline">creator</span>.  Here is the above gdbm example with <span class="codeline">ThreadLocal()</span>, using an anonymous (lambda) creation function to automatically allocate a new <span class="codeline">gdbm</span> handle per thread:</p>
	

<div class="code">
<pre><span class="blocktag">&lt;%python scope="global"&gt;</span>
<span class="python_operator">    </span><span class="python_keyword">import </span><span class="python_name">gdbm</span><span class="python_operator">
    </span><span class="python_name">db </span><span class="python_operator">= </span><span class="python_name">ThreadLocal</span><span class="python_enclosure">(</span><span class="python_name">creator </span><span class="python_operator">= </span><span class="python_keyword">lambda</span><span class="python_operator">: </span><span class="python_name">gdbm</span><span class="python_operator">.</span><span class="python_name">open</span><span class="python_enclosure">(</span><span class="python_literal">"weather.dbm"</span><span class="python_operator">, </span><span class="python_literal">'r'</span><span class="python_enclosure">))</span><span class="python_operator">
</span><span class="blocktag">&lt;/%python&gt;</span><span class="text">

</span><span class="argstag">&lt;%args&gt;</span>
<span class="python_operator">    </span><span class="python_name">zipcode</span><span class="python_operator">
</span><span class="argstag">&lt;/%args&gt;</span><span class="text">

temperature in your zip for today is: </span><span class="substitution">&lt;%</span><span class="python_operator"> </span><span class="python_name">db</span><span class="python_enclosure">()[</span><span class="python_name">zipcode</span><span class="python_enclosure">]</span><span class="python_operator"></span><span class="substitution">%&gt;</span></pre></div>

	
	
    </div>


    <a href="#scopedpython" class="toclink">back to section top</a>


</div>





</div>




    
<div class="sectionnavblock">
<div class="sectionnav">


        Previous: <a href="embedding.html">Embedding Python in HTML</a>

                |

        Next: <a href="globals.html">Standard Global Variables</a>

</div>
</div>


</div>


		</div>






</div>

















<center>
</center>
</body>
</html>