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()










