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
| ## from threading import Thread
## from threading import Lock
from multiprocessing import Process as Thread
from multiprocessing import Lock
import time
print 'run globals'
class MyPrint:
def __init__(self):
self._lock = Lock()
def __call__(self, m):
with self._lock:
print (m)
def counter(text, count, myprint):
for i in range(count):
myprint('%s-%d' % (text, i))
time.sleep(0.0) ## force resched
if __name__ == '__main__':
myprint = MyPrint() # la console...
ta = Thread(target=counter, args=('aaa', 10, myprint))
ta.start()
tb = Thread(target=counter, args=('bbb', 10, myprint))
tb.start()
ta.join()
tb.join() |