Olá,
para variar um pouco uma questão sobre ruby :-)
Estava fazendo algums testes com o uso de threads em ruby e esbarrei
no sequinte problema: preciso acessar vários recursos do sistema
operacional ao mesmo tempo sendo que alguns deles bloqueiam o
interpretador. Pergunta: existe alguma forma de fazer isso sem criar
novos processos com novos interpretadores para cada processo ou isso é
mesmo uma limitação do Ruby pelo fato dele não suportar threads
nativas?
Exemplo:
Tenho 2 arquivos: test1, test3
e um named pipe: test2
[EMAIL PROTECTED]:$ ls -l / > test1
[EMAIL PROTECTED]:$ ls -l / > test3
[EMAIL PROTECTED]:$ mkfifo test2
O programa threads.rb (transcrito abaixo) cria 3 threads acessando
cada recurso. test3 só é lido após desbloquear o pipe. ( com por
exemplo "echo oi > test2 )
[EMAIL PROTECTED]:$ ./threads.rb
start reading file: test1
start reading file: test2
Em python isso não acontece:
[EMAIL PROTECTED]:$ ./threads.py
<MyThread(Thread-1, started)>: start reading file: test1
<MyThread(Thread-1, started)>: end reading file: test1
<MyThread(Thread-2, started)>: start reading file: test2
<MyThread(Thread-3, started)>: start reading file: test3
<MyThread(Thread-3, started)>: end reading file: test3
<MyThread(Thread-2, started)>: end reading file: test2
Obrigado de antemão,
Tácio
PS:
#!/usr/bin/env ruby
files = %w( test1 test2 test3 )
threads = []
for file in files
threads << Thread.new(file){ |myfile|
puts "start reading file: " + myfile
File.read(myfile)
puts "end reading file: " + myfile
}
end
threads.each { |t| t.join }
# EOF -------------------------------------------------
#!/usr/bin/python
import threading
class MyThread ( threading.Thread ):
def __init__( self, file ):
self.file = file
threading.Thread.__init__ ( self )
def run ( self ):
print str(self)+ ': start reading file: ' +self.file
f=open(self.file).read()
print str(self)+ ': end reading file: ' +self.file
files=['test1','test2','test3']
threads=[]
for f in files:
t=MyThread(f)
threads.append(t)
t.start()
for t in threads:
t.join()
# EOF -------------------------------------------------
_______________________________________________
Ruby-l mailing list
[email protected]
http://www.listas.unicamp.br/mailman/listinfo/ruby-l