-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreads.py
More file actions
63 lines (46 loc) · 1.33 KB
/
Threads.py
File metadata and controls
63 lines (46 loc) · 1.33 KB
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
#!/usr/bin/env python
# encoding: utf-8
"""
Threads.py
Created by Marko on 2010-05-23.
Copyright (c) 2010 Bstards. All rights reserved.
"""
"""
Los hilos en Python son controlados por Global Interpreter Lock GIL,
este modulo solo permite la ejecucion de un solo Thread, lo que posibilita
la facilidad de creacion de extensiones en C pero tiene gran desventaja
en el rendimiento de la app, por lo cual es muy cuestionado el uso de
Threads y mejor se haga uso de procesos.
Otra opcion es usar Jython o IronPython ya que estos no tienen el GIL
"""
import threading
class MiThread( threading.Thread ):
def __init__( self, num ):
threading.Thread.__init__(self)
self.num = num
def run( self ):
print "Soy el hilo: ", self.num, ", name: ", self.name
# print "threading.activeCount: ", threading.activeCount
print "Hilo principal======================\n"
for i in range(0, 10):
t = MiThread( i )
t.start()
t.join()
print "\n++++++++++++++++Sincronizacion++++++++++++++++"
def syncronized( lock ):
def dec( f ):
def funcDec(*args, **kwargs):
lock.acquire()
try:
return f(*args, **kwargs)
finally:
lock.release()
return funcDec
return dec
class MyThreadDecorated( threading.Thread ):
@syncronized(threading.Lock())
def run(self):
print "Metodo Sicronizado...."
t = MyThreadDecorated()
t.start()
t.join()