0

I have a python program which dynamically move and rename files into a hadoop cluster. The files usually range from 10mb(parsed) up to 1.5gb (raw data). For the move commands to finish it can take a while and from what I can tell python races through them and none of the move commands get to finish. What is the proper way to have python wait for previous commands. I store the commands in a variable and pass it to os.system. The relevant code is

os.system(moverawfile)
os.system(renamerawfile)
os.system(moveparsedfile)
os.system(renameparsedfile)

I know rename commands are done basically instantaneously. Am I not supposed to use os.system? How do i ensure that python will wait for each command to finish before moving onto the next one.

4
  • What is your exact code? os.system does not return until the command it spawns exits. Commented Mar 2, 2016 at 3:41
  • 1
    You should be using subprocess anyways. You can have an exception thrown on a command error, for example. Commented Mar 2, 2016 at 3:44
  • os.system just calls system(3), and that waits for the command to complete. Commented Mar 2, 2016 at 3:48
  • hadoop fs -put rawjsondata.txt /home/hadoop/project/March/raw/rawjsondata.txt thats the entirety of "moverawfile" Commented Mar 2, 2016 at 3:51

1 Answer 1

1

I would suggest that you use run from subprocess as per Python documentation. It waits for your command to complete before returning.

Sign up to request clarification or add additional context in comments.

4 Comments

Some methods in subprocess wait -- others don't. . .
whats the proper syntax of subprocess?
os.system already waits; if it's behaving asynchronously with os.system, subprocess won't fix that (even though it's a good idea in general).
@Sam: For your example command, you'd do something like subprocess.check_call(['hadoop', 'fs', '-put', localfilepath, remotefilepath]) (I'm assuming the file paths are in variables somewhere). Use plain subprocess.call if you don't want it to raise an exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.