Code

Changes between Version 44 and Version 45 of DjangoSpecifications/Core/Threading


Ignore:
Timestamp:
04/26/08 13:46:55 (6 years ago)
Author:
mrts
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DjangoSpecifications/Core/Threading

    v44 v45  
    1212Only easy-to-identify globals have been reviewed, a related task is to identify other components not listed here that may have threading issues. 
    1313 
    14 == Globals == 
    15  
    16 There are four types of globals: 
    17  1. globals that are assigned to at module level and never modified later (THREAD-SAFE), 
    18  1. globals that are assigned to at module level and whose elements are modified with module level code, but never modified later (PROBABLY THREAD-SAFE, although elementwise modification at module level is not thread-safe ''per se'', the module is most likely cached ''before'' threads get access to it) 
    19  1. global mutable data structures (lists and dictionaries, also instances) that are assigned to at module level but whose elements are modified in functions and that are accessed without using the `global` keyword (NOT THREAD-SAFE), 
    20  1. globals assigned to in functions by using the `global` keyword (NOT THREAD-SAFE), 
     14== Introduction == 
     15 
     16The main pattern of global variable usage in Django is ''use `var` if initialized, otherwise initialize''. This is generally not thread-safe. 
    2117 
    2218"Not thread-safe" has two broad subcategories: 
    23  * inefficiencies due to calls meant to occur only once occurring more than once (the general `if not foo: initialize foo`, including `memoize` decorator), 
     19 * inefficiencies due to initialization calls meant to occur only once occurring more than once (the general `if not foo: initialize foo`, including `memoize` decorator), 
    2420 * errors due to incomplete initialization. 
     21 
     22Paradoxically, accepting the "inefficiencies" results generally in more ''efficient'' execution as: 
     23 * mutual exclusion (locking) is expensive, 
     24 * the inefficient case is not a certain event, it only occurs with a probability that can be quite low, 
     25 * locking would needlessly penalize single-threaded execution. 
     26 
     27Thus, lock-free algorithms should be always preferred and the inefficient case tolerated -- unless the inefficiency is highly probable and results in overhead that is considerably greater than with locking. 
    2528 
    2629=== Inefficiencies === 
     
    7477 
    7578There is at least one location in code that uses the "bad" algorithm. 
     79 
     80 
     81== Globals == 
     82 
     83There are four types of globals: 
     84 1. globals that are assigned to at module level and never modified later (THREAD-SAFE), 
     85 1. globals that are assigned to at module level and whose elements are modified with module level code, but never modified later (PROBABLY THREAD-SAFE, although elementwise modification at module level is not thread-safe ''per se'', the module is most likely cached ''before'' threads get access to it) 
     86 1. global mutable data structures (lists and dictionaries, also instances) that are assigned to at module level but whose elements are modified in functions and that are accessed without using the `global` keyword (NOT THREAD-SAFE), 
     87 1. globals assigned to in functions by using the `global` keyword (NOT THREAD-SAFE), 
    7688 
    7789=== Modules' use of globals ===