Skip to content

Instantly share code, notes, and snippets.

@Kerrigan29a
Last active September 12, 2018 08:36
Show Gist options
  • Save Kerrigan29a/4f459c901a21951ad1bd to your computer and use it in GitHub Desktop.
Save Kerrigan29a/4f459c901a21951ad1bd to your computer and use it in GitHub Desktop.
Executing Python tests that use multiprocessing module in Windows
#!/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())
C:\multiprocessing_unittest>python -m unittest discover
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
.
----------------------------------------------------------------------
Ran 1 test in 0.219s
OK
#!/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