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 | |
| 16 | The main pattern of global variable usage in Django is ''use `var` if initialized, otherwise initialize''. This is generally not thread-safe. |
| 21 | |
| 22 | Paradoxically, 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 | |
| 27 | Thus, 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. |
| 79 | |
| 80 | |
| 81 | == Globals == |
| 82 | |
| 83 | There 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), |