question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

it will execute multiple times when import from test.py

See original GitHub issue

In my commamd line, the ‘test.py’ works well .

I want use this file in other python file, So I add a function to ‘test.py’, it is:

    def run(self, imgs=None, model="config/ade20k-resnet50dilated-ppm_deepsup.yaml", gpu=0):
        if imgs == None:
            return False
        cfg.merge_from_file(model)

        cfg.MODEL.arch_encoder = cfg.MODEL.arch_encoder.lower()
        cfg.MODEL.arch_decoder = cfg.MODEL.arch_decoder.lower()

        # absolute paths of model weights
        cfg.MODEL.weights_encoder = os.path.join(
            cfg.DIR, 'encoder' + cfg.TEST.suffix)
        cfg.MODEL.weights_decoder = os.path.join(
            cfg.DIR, 'decoder' + cfg.TEST.suffix)

        if not os.path.exists(cfg.MODEL.weights_encoder):
            return  (False, "checkpoint does not exitst!")

        # generate testing image list
        if os.path.isdir(imgs[0]):
            imgs = find_recursive(imgs[0])
        else:
            imgs = [imgs]
        assert len(imgs), "imgs should be a path to image (.jpg) or directory."
        cfg.list_test = [{'fpath_img': x} for x in imgs]
        if not os.path.isdir(cfg.TEST.result):
            os.makedirs(cfg.TEST.result)
        
        main(cfg, gpu)

However, In another file, when I use this function like this:

import run from test
run("test.jpg")

It outputs error:

Loading weights for net_encoder
Loading weights for net_decoder
# samples: 1
Loading weights for net_encoder
Loading weights for net_encoder
Loading weights for net_encoder
Loading weights for net_encoder
Loading weights for net_encoder
Loading weights for net_decoder
Loading weights for net_decoder
Loading weights for net_decoder
# samples: 1
Loading weights for net_decoder
Loading weights for net_decoder
# samples: 1
# samples: 1
# samples: 1
......

then my command line is crash.

However, to use this function in the file test.py works well. Only in other file will it failed.

This function is as same as the code in the if __name__ == '__main__':, so where is wrong?

Thanks.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
acdzhcommented, Jul 18, 2019

I finally find why: I try to print the __name__ in the begin of the function run(). At first it print __main__, after that it print many __mp_main__. It is the module multiprocess 's problem and only occurs on Windows.

Those too many new processes is the reason why my cammand line crash.

Solution: in other file, please judge if __name__ == '__main__': every time, like this:

import run from test
if __name__ == '__main__':
    run('img/test.jpg')

This problem just occurs on Windows.

For more can see multiprocess , torch.multiprocessing

2reactions
hangzhaomitcommented, Jul 18, 2019

@acdzh maybe I have a bug here, can you try to replace imgs[0] with imgs?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I repeat each test multiple times in a py.test run?
I attempted to do this in the pytest_collection_modifyitems() hook. I modified the list of items passed in, to specify each item more than...
Read more >
How to correctly run pytest.main() programmatically multiple ...
If I invoke pytest.main(['test_foo.py']) multiple time from the same running script, it will give the same result, even if test_foo.py ...
Read more >
pytest-repeat - PyPI
Each test collected by pytest will be run count times. If you want to mark a test in your code to be repeated...
Read more >
Pytest Tutorial: Executing Multiple Test Cases From Single File
With pytest you can run multiple test cases from a single file, or even run selective tests with custom markers or by grouping...
Read more >
Usage and Invocations — pytest documentation
Pytest supports several ways to run and select tests from the command-line. Run tests in a module. pytest test_mod.py. Run tests in a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found