1

Given below is the control flow of my python code:

from BB import B
def A(param):
    ...
    ...
    //This takes a while to complete execution
    ...
    B()
print something
A(param) //Function call

The problem here is that the function B is called before A can complete its execution. A creates and write to a file, while B reads from it and performs some operation. B being called after the completion of A is pretty important.

I tried this, but it dint solve the issue.

from BB import B
def A(param):
    ...
    ...
    //This takes a while to complete execution
    ...
print something
A(param) //Function call
B()
  • Are you familiar with the callback concept? http://stackoverflow.com/questions/4689984/implementing-a-callback-in-python-passing-a-callable-reference-to-the-current – Joze May 03 '16 at 06:22
  • Did you possibly forget to close the file after A() has written to it? – Byte Commander May 03 '16 at 06:28

2 Answers2

0

It sounds like you need to make A a blocking call -- i.e. avoid returning until the process is done. If it's writing to a file, you might consider the following:

import os

from time import sleep

def A(param):

    filepath = "/path/to/output/file"
    ... do stuff ...
    while not os.path.exists(filepath):
        sleep(5)
    B()

However, as @Joze comments, you might want to use a callback strategy just for general composition reasons.

Thtu
  • 1,992
  • 15
  • 21
  • Blocking call isn't helping, and along with that I get a Value Error due to some file opening/closing issue. I'm not able to figure out how and where to include the callback strategy. – RowenaRavenclaw May 03 '16 at 07:54
  • @RowenaRavenclaw, it sounds like you have an issue with the part of your code that writes the file. I would recommend sharing that snippet. – Thtu May 03 '16 at 07:58
  • Thanks! The file is actually written by redirecting the output from the console. I then ssh to another server where I do something while the 'print' statements on the console get redirected to this file. In the other server where I ssh to, I read and write from and to a few other files. – RowenaRavenclaw May 03 '16 at 08:20
  • Then it sounds like your code is trying to access the file while it's being written to. In this situation, you need a way to poll for the state of the file. This is the annoying thing about relying on side effects from within your code. Is there any way you can re-write your code to write the file using python rather than manipulating stdout? Otherwise, if you know that there's a max on how long it takes to write to this file, then just sleep for that long. – Thtu May 04 '16 at 21:18
  • I ssh to a server and run the script. So in order to capture the logs, I had to redirect the stdout. If I do any other file operation, it gets written to a file on the server that I ssh into... – RowenaRavenclaw May 07 '16 at 11:04
0

Brute forcing it, you could place a timer in before B() does some reading. import time time.sleep(5) # delays for 5 seconds

Or tweak the 5 seconds to be the shortest time you expect that will still work

Tom Mozdzen
  • 338
  • 4
  • 16