caute mam dve vlakna ktore som spravil z funkcii (def) a chcem sa opytat ci sa da nejakym sposobom uzamknut jedno vlakno druhym iba na chvilu ...dakujem vopred za odpovede
Fórum › Python
Blockovanie vlakna

Doporučuji modul Threading (normálně už je v Pythonu), je poněkud složitější než obyčejný thread, ale na netu se dá najít hromada dokumentace.
Mám tu krásnej příklad na práci s modulem Threading (alespoň mně se líbí), je to skript na generování prvočísel, hlavní vlákno počítá, vedlejší vlákno zobrazuje, jak to vypadá. Algoritmus je pravděpodobně z Wikipedie. Zkoušel jsem to na Windowsech a funguje. Škoda, že je v tom takovej bordel. K tvému problému doporučuju Threading.Lock, kde když dvě vlákna zavolají lock.acquire(), pustí to dál jen jedno, druhé čeká, až v prvním proběhne lock.release()
import time,threading,sys
class show(threading.Thread):
def __init__(self,lock,data,limit):
threading.Thread.__init__(self)
self.lock=lock
self.data=data
self.limit=(str(limit-1),str(int(limit**0.5)))
self.lens=(len(self.limit[0]),len(self.limit[1]))
self.d=sum(self.lens)*2+8
def run(self):
while True:
lock.acquire()
a,b,c=self.data
lock.release()
if not a:
sys.stdout.write("\x08"*self.d+\
self.limit[1]+" / "+self.limit[1]+\
"; "+self.limit[0]+" / "+self.limit[0]+"\n")
break
sys.stdout.write("\x08"*self.d+\
str(b).rjust(self.lens[1])+" / "+self.limit[1]+\
"; "+str(c).rjust(self.lens[0])+" / "+self.limit[0])
time.sleep(1)
bad_input=True
while bad_input:
limit=raw_input("Find primes up to ")
try:limit=int(limit)+1
except:pass
else:bad_input=limit<2
started=time.time()
print "preparing variables..."
is_prime=[]
c2=limit/1000000
c4=str(c2)
c3=len(str(c2))
sys.stdout.write("0".rjust(c3)+" / "+c4)
for c1 in xrange(c2):
is_prime+=list([True]*1000000)
sys.stdout.write("\x08"*(c3*2+3)+str(c1).rjust(c3)+" / "+c4)
is_prime+=list([True]*(limit%1000))
sys.stdout.write("\x08"*(c3*2+3)+c4+" / "+c4+"\n")
s=int(limit**0.5)+1
lock=threading.Lock()
data=[True,2,0]
thr=show(lock,data,limit)
thr.start()
print "generating..."
while data[1]<s:
if is_prime[data[1]]:
lock.acquire()
data[2]=data[1]**2
lock.release()
while data[2]<limit:
is_prime[data[2]]=False
data[2]+=data[1]
lock.acquire()
data[1]+=1
lock.release()
data[0]=False
thr.join()
est=int((time.time()-started)*1000)
print "generated in "+str(est)+" ms"
print "formating output..."
primes=[]
k=100000
m=limit/k
sm=str(m)
lm=len(sm)
c1=0
for c1 in xrange(m):
for c2 in xrange(k):
act=c1*k+c2
if is_prime[act]:
primes.append(act)
sys.stdout.write("\x08"*(lm*2+3)+str(c1).rjust(lm)+" / "+sm)
for c2 in xrange(limit%k):
act=c1*k+c2
if is_prime[act]:
primes.append(act)
sys.stdout.write("\x08"*(lm*2+3)+sm+" / "+sm+"\n")
l=len(primes)
print "found "+str(l)+" primes"
if l:
print "largest is "+str(primes[-1])
print "saving generated primes into file primes"+str(limit-1)+".txt"
out="\r\n".join(str(n) for n in primes)
fle=file("primes"+str(limit-1)+".txt","wb")
fle.write(out)
fle.close()
print "write \"q\" to quit or something else to show generated primes"
if raw_input()!="q":
print out
print "press RETURN to quit"
raw_input()
Koumes21

Přidej příspěvek
Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×






























































Uživatelé prohlížející si toto vlákno
Uživatelé on-line: 0 registrovaných, 16 hostů
Podobná vlákna
Moderátoři diskuze