.


:




:

































 

 

 

 


Threading ң




қ threading ғ қ ғ ү :

activeCount(). қ қ Thread ң ң қ.қ ү, ұ len(threading.enumerate()).

currentThread(). ғғ -ғ қ, ғ қғ, қ ғ ә. ғ threading қ құ, ққғ қ(dummy thread object) -ғ қ .

enumerate(). ғ қ. қғ ә ә ғ ғ .

Thread

threading.Thread ң Python-ғң ғ ұ. ғ ә ә : ң ә ғ ұ ө қғ run() ә ң . ә ғғ қғ. threading.Thread :

Thread(group, target, name, args, kwargs)

ұғ group ғ ғ (ә қ, None ң ), target - run() ә қ , name ғ , args ә kwargs target қ ү қ ә қ ң (ә) ә ө. ғ қ қғ, қ ә қ ғ :

import threading

def proc(n):

print "", n

p1 = threading.Thread(target=proc, name="t1", kwargs={"n": "1"})

p2 = threading.Thread(target=proc, name="t2", kwargs={"n": "2"})

p1.start()

p2.start()

ә threading ұ қ ғ . ә run() әң қ Thread:

import threading

class T(threading.Thread):

def __init__(self, n):

threading.Thread.__init__(self, name="t" + n)

self.n = n

def run(self):

print "", self.n

p1 = T("1")

p2 = T("2")

p1.start()

p2.start()

ң қ қ ң қ. ұғ ғ қ ү, run() ә әң ә, -ғң start() ә қ.

Ө ғ ә қ қ қғ :

start(). ғғ ө .

run(). ұ ә ғ ғ ә ұ.

join([timeout]). ғ қ ғ қғ ә ғң қ ү қ. timeout ( ү ) ү қ ө ү (), ғ ң, қ join ә қғ, ғң қ қ қғ ғ ө ұ ғ. ғң join() ө қғ . ғ ө-ө қ . ә ә қғ ғң қ ү . "join" ө ғ ғ ғғ , ғ join() қ ә, ғ қғ ң ғ қ ә қ қ.

getName(). ғ қ. ғ ү ұ "MainThread".

setName(name). ғғ name .

isAlive(). ғ ұ , ққ қ (run() ә қғ, қ ә қ).

isDaemon(). ғ , ққ қ. Python-ғ ғ, , қ ғң қ қ. ғ .

setDaemon(daemonic). ғ daemonic . ұ ң қ ә ә ғ .

Thread ә Java- ғғ ә ү ғ (ғ қ, қ ә қ ғң ү, ңққ ә қ ә), құ.

threading.Timer қ қ ә ұ. ұ threading.Thread ң , қ start() ә қ. ғ Hello, world! қ ғ ү:

def hello():

print "Hello, world!"

t = Timer(30.0, hello)

t.start()

Қ құ threading ң Lock ү. Құ ү : қ ұ қ ұ ү. ңғ ғ ғ ғ . Lock ң ә :

acquire([blocking=True]). Құ құғ ұ . blocking ө ққ , ғ құң ү . , ә ә қ. blocking ә ққ , ә True қ (құ ә ң ). құ қ (ғ blocking=False ), ә True қ, құ ә ғ ә ң. ғ False қ .

release(). Құ ү ұ.

locked(). Құң ғғ ү қ (True - қ, False - қ).

, құ ү ң ғ ү , ұ ү ғ қ .

Құң, threading.Lock- ғ ң құ ө ұғ , ғ ұқ - threading.Rlock. ұ құ құ қ құ , . ұ ү , .





:


: 2017-01-28; !; : 795 |


:

:

, , .
==> ...

1896 - | 1536 -


© 2015-2024 lektsii.org - -

: 0.011 .