U E D R , A S I H C RSS

방울뱀스터디/Thread

Thread

() .
. .~~!

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

lock = thread.allocate_lock() # lock . (->)
#
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()



Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:35
Processing time 0.0168 sec