dbtest_multiproc.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/env python
  2. from db_test import DBDakTestCase
  3. from daklib.dbconn import DBConn
  4. from multiprocessing import Pool
  5. from time import sleep
  6. import unittest
  7. def read_number():
  8. session = DBConn().session()
  9. result = session.query('foo').from_statement('select 7 as foo').scalar()
  10. sleep(0.1)
  11. session.close()
  12. return result
  13. class MultiProcTestCase(DBDakTestCase):
  14. """
  15. This TestCase checks that DBConn works with multiprocessing.
  16. """
  17. def save_result(self, result):
  18. self.result += result
  19. def test_seven(self):
  20. '''
  21. Test apply_async() with a database session.
  22. '''
  23. self.result = 0
  24. pool = Pool()
  25. pool.apply_async(read_number, (), callback = self.save_result)
  26. pool.apply_async(read_number, (), callback = self.save_result)
  27. pool.apply_async(read_number, (), callback = self.save_result)
  28. pool.apply_async(read_number, (), callback = self.save_result)
  29. pool.apply_async(read_number, (), callback = self.save_result)
  30. pool.close()
  31. pool.join()
  32. self.assertEqual(5 * 7, self.result)
  33. if __name__ == '__main__':
  34. unittest.main()