Thread ¶
그() .
게 경 고 경 . 각각 고 .~~!
게 경 고 경 . 각각 고 .~~!
고)
(process) : 기 개 , 그 각각 그 ..
개 .
(process) : 기 개 , 그 각각 그 ..
개 .
Thread ¶
: 고 start_new_thread() 그 .
1 ¶
~cpp
import thread
i=0
j=0
def f():
global i
while 1: i+=1
def g():
global j
while 1: j+=1
thread.start_new_thread(f,())
thread.start_new_thread(g,())
print 'i=',i
print 'j=',j
print 'i=',i
print 'j=',j
2 ¶
~cpp
import thread, time
def counter(id):
for i in range(5):
print 'id %s --> %s' % (id, i)
time.sleep(0.1)
for i in range(5):
thread.start_new_thread( counter, (i,) )
# 5개 각각
time.sleep(2) # 기
print 'Exiting'
공 ¶
- 공 .
경고 길 .
값 갱 간 교 길경 기 .
3 ¶
~cpp
import thread, time
g_count = 0
def counter(id, count):
global g_count
for i in range(count):
print 'id %s -> %s' % (id, i)
g_count = g_count +1
for i in range(5):
thread.start_new_thread(counter,(i,5))
time.sleep(3)
print 'Total Counter =', g_count
print 'Exitintg'
()
결 갱 그 근 ~!
allow_lock() 객 겨.(3)
1. thread.acquire() - . .
2. thread.release() - . 갈 .
3. thread.locked() - 1, 0 .
결 갱 그 근 ~!
allow_lock() 객 겨.(3)
1. thread.acquire() - . .
2. thread.release() - . 갈 .
3. thread.locked() - 1, 0 .
lock = thread.allocate_lock() #기 lock 공 . (->)
# 과
lock.acquire() # 고 간. 까 기 기.
g_count = g_count+1 #
lock.release() # . ㅇ 갈 .
# 과
lock.acquire() # 고 간. 까 기 기.
g_count = g_count+1 #
lock.release() # . ㅇ 갈 .
4 ¶
~cpp
import thread, time
g_count = 0
lock = thread.allocate_lock()
def counter(id, count):
global g_count
for i in range(count):
print 'id %s -> %s' % (id, i)
lock.acquire()
g_count = g_count +1
lock.release()
for i in range(5):
thread.start_new_thread(counter,(i,5))
time.sleep(3)
print 'Total Counter =', g_count
print 'Exitintg'
~cpp
from Tkinter import *
import time, thread
def CountTime():
global i
i=0
count=0
while count<80:
text.insert(1.0, i)
text.delete(INSERT)
#text.update()
time.sleep(0.1)
count=count+1
root = Tk()
root.protocol("WM_DELETE_WINDOW", exit)
text=Text(root, height=1)
text.pack()
thread.start_new_thread(CountTime,())
canvas = Canvas(root, width=400, height=300)
canvas.pack()
wall = PhotoImage(file='wall.gif')
canvas.create_image(0, 0, image=wall, anchor=NW)
stop=0
root.mainloop()










