A Thread modul

Ez a modul a többszálú programok készítésére szolgál – a threading modullal egyetemben.

Egy egyszerű többszálú program

# File: thread_a_szalak_egyszeru_kiprobalasa.py import thread import time, random def feladat(): for i in range(50): # ugy teszunk, mintha dolgoznank, 10-100 ms idotartamig time.sleep(random.randint(10, 100) / 1000.0) print thread.get_ident(), "-- a", i, "ciklus befejezodott" for i in range(2): thread.start_new_thread(feladat, ()) # ebben a pillanatban a programnak van # - egy legfelso szintu szalja # - ket gyermek-szalja # ha a foszal leallna, a gyermekek is megallnak. ezert kell a sleep. # hogy a foszal fusson, eletben legyen addig, amig a gyerekek dolgoznak. time.sleep(6) print "A fo programszal vegetert!"

# File: thread_a_szalak_egyszeru_kiprobalasa.out 1940 -- a 0 ciklus befejezodott 3476 -- a 0 ciklus befejezodott 3476 -- a 1 ciklus befejezodott 1940 -- a 1 ciklus befejezodott 3476 -- a 2 ciklus befejezodott 3476 -- a 3 ciklus befejezodott 1940 -- a 2 ciklus befejezodott 3476 -- a 4 ciklus befejezodott 1940 -- a 3 ciklus befejezodott 1940 -- a 4 ciklus befejezodott ... ... 3476 -- a 44 ciklus befejezodott 1940 -- a 47 ciklus befejezodott 3476 -- a 45 ciklus befejezodott 1940 -- a 48 ciklus befejezodott 3476 -- a 46 ciklus befejezodott 1940 -- a 49 ciklus befejezodott 3476 -- a 47 ciklus befejezodott 3476 -- a 48 ciklus befejezodott 3476 -- a 49 ciklus befejezodott A fo programszal vegetert!

Post Comments

You must be logged in to post a comment.