Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4619

Open3.popen3 / how to print the output

$
0
0

I have a little ruby script which does a mysql import in the way: mysql -u <user> -p<pass> -h <host> <db> < file.sql, but utilizes Open3.popen3 to do so. That is what I have so far:

mysqlimp = "mysql -u #{mysqllocal['user']} "mysqlimp << "-h #{mysqllocal['host']} "mysqlimp << "-p#{mysqllocal['pass']} "mysqlimp << "#{mysqllocal['db']}"Open3.popen3(mysqlimp) do |stdin, stdout, stderr, wthr|  stdin.write "DROP DATABASE IF EXISTS #{mysqllocal['db']};\n"  stdin.write "CREATE DATABASE #{mysqllocal['db']};\n"  stdin.write "USE #{mysqllocal['db']};\n"  stdin.write mysqldump #a string containing the database data  stdin.close  stdout.each_line { |line| puts line }  stdout.close  stderr.each_line { |line| puts line }  stderr.closeend

That is actually doing the Job, but there is one thing that bothers me, concerned to the output I would like to see.

If I change the first line to:

mysqlimp = "mysql -v -u #{mysqllocal['user']} " #note the -v

then the whole script hangs forever.

I guess, that happens because the read- and write-stream block each other and I also guess that the stdout needs to be flushed regularly so that stdin will go on to be consumed. In other words, as long as the buffer of the stdout is full, the process will wait until its flushed, but since this is done at the very bottom first, that never happens.

I hope someone can verify my theory? How could I write code that does prints out everything from the stdout and writes everything to the stdin as well?


Viewing all articles
Browse latest Browse all 4619

Trending Articles