Last active
September 12, 2018 08:36
-
-
Save Kerrigan29a/4f459c901a21951ad1bd to your computer and use it in GitHub Desktop.
Executing Python tests that use multiprocessing module in Windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from multiprocessing import Pool | |
def f(x): | |
return x*x | |
def run(): | |
pool = Pool(processes=4) # start 4 worker processes | |
print pool.map(f, range(10)) # prints "[0, 1, 4,..., 81]" | |
return 0 | |
if __name__ == '__main__': | |
exit(run()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
C:\multiprocessing_unittest>python -m unittest discover | |
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81] | |
. | |
---------------------------------------------------------------------- | |
Ran 1 test in 0.219s | |
OK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import unittest | |
import app | |
import sys | |
class TestExtractReturnsDataset(unittest.TestCase): | |
def test(self): | |
# Very dirty hack | |
# To fix the Windows forking system it's necessary to point __main__ to | |
# the module we want to execute in the forked process | |
old_main = sys.modules["__main__"] | |
old_main_file = sys.modules["__main__"].__file__ | |
sys.modules["__main__"] = sys.modules["app"] | |
sys.modules["__main__"].__file__ = sys.modules["app"].__file__ | |
result = app.run() | |
sys.modules["__main__"] = old_main | |
sys.modules["__main__"].__file__ = old_main_file | |
self.assertEqual(result, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment