1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
|
import os
import sys
import time
import pickle
import signal
import asyncio
import unittest
import threading
import concurrent
import dataclasses
import multiprocessing
from concurrent.futures import CancelledError, TimeoutError
import pebble
from pebble import ProcessPool, ProcessExpired
from pebble.pool.base_pool import PoolStatus
# set start method
supported = False
mp_context = None
methods = multiprocessing.get_all_start_methods()
if 'fork' in methods:
try:
mp_context = multiprocessing.get_context('fork')
if mp_context.get_start_method() == 'fork':
supported = True
except RuntimeError: # child process
pass
initarg = 0
def initializer(value):
global initarg
initarg = value
def long_initializer():
time.sleep(60)
def broken_initializer():
raise BaseException("BOOM!")
def function(argument, keyword_argument=0):
"""A docstring."""
return argument + keyword_argument
def initializer_function():
return initarg
def error_function():
raise BaseException("BOOM!")
def return_error_function():
return BaseException("BOOM!")
@dataclasses.dataclass(frozen=True)
class FrozenError(Exception):
pass
def frozen_error_function():
raise FrozenError()
def pickle_error_function():
return threading.Lock()
def long_function(value=1):
time.sleep(value)
return value
def pid_function():
time.sleep(0.1)
return os.getpid()
def sigterm_function():
signal.signal(signal.SIGTERM, signal.SIG_IGN)
time.sleep(10)
def suicide_function():
os._exit(1)
def process_function():
p = multiprocessing.Process(target=function, args=[1])
p.start()
p.join()
return 1
def pool_function():
pool = multiprocessing.Pool(1)
result = pool.apply(function, args=[1])
pool.close()
pool.join()
return result
def pebble_function():
with ProcessPool(max_workers=1) as pool:
f = pool.schedule(function, args=[1])
return f.result()
@unittest.skipIf(not supported, "Start method is not supported")
class TestProcessPool(unittest.TestCase):
def setUp(self):
global initarg
initarg = 0
self.event = threading.Event()
self.event.clear()
self.result = None
self.exception = None
def callback(self, future):
try:
self.result = future.result()
except BaseException as error:
self.exception = error
finally:
self.event.set()
def test_process_pool_single_future(self):
"""Process Pool Fork single future."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.schedule(function, args=[1],
kwargs={'keyword_argument': 1})
self.assertEqual(future.result(), 2)
def test_process_pool_multiple_futures(self):
"""Process Pool Fork multiple futures."""
futures = []
with ProcessPool(max_workers=2, context=mp_context) as pool:
for _ in range(5):
futures.append(pool.schedule(function, args=[1]))
self.assertEqual(sum([f.result() for f in futures]), 5)
def test_process_pool_callback(self):
"""Process Pool Fork result is forwarded to the callback."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.schedule(
function, args=[1], kwargs={'keyword_argument': 1})
future.add_done_callback(self.callback)
self.event.wait()
self.assertEqual(self.result, 2)
def test_process_pool_error(self):
"""Process Pool Fork errors are raised by future get."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.schedule(error_function)
self.assertRaises(BaseException, future.result)
def test_process_pool_error_returned(self):
"""Process Pool Fork returned errors are returned by future get."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.schedule(return_error_function)
self.assertIsInstance(future.result(), BaseException)
def test_process_pool_error_callback(self):
"""Process Pool Fork errors are forwarded to callback."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.schedule(error_function)
future.add_done_callback(self.callback)
self.event.wait()
self.assertTrue(isinstance(self.exception, BaseException))
def test_process_pool_pickling_error_task(self):
"""Process Pool Fork task pickling errors
are raised by future.result."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.schedule(function, args=[threading.Lock()])
self.assertRaises((pickle.PicklingError, TypeError), future.result)
def test_process_pool_pickling_error_result(self):
"""Process Pool Fork result pickling errors
are raised by future.result."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.schedule(pickle_error_function)
self.assertRaises((pickle.PicklingError, TypeError), future.result)
def test_process_pool_frozen_error(self):
"""Process Pool Fork frozen errors are raised by future get."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.schedule(frozen_error_function)
self.assertRaises(FrozenError, future.result)
def test_process_pool_timeout(self):
"""Process Pool Fork future raises TimeoutError if so."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.schedule(long_function, timeout=0.1)
self.assertRaises(TimeoutError, future.result)
def test_process_pool_timeout_callback(self):
"""Process Pool Fork TimeoutError is forwarded to callback."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.schedule(long_function, timeout=0.1)
future.add_done_callback(self.callback)
self.event.wait()
self.assertTrue(isinstance(self.exception, TimeoutError))
def test_process_pool_cancel(self):
"""Process Pool Fork future raises CancelledError if so."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.schedule(long_function)
time.sleep(0.1) # let the process pick up the task
self.assertTrue(future.cancel())
self.assertRaises(CancelledError, future.result)
def test_process_pool_cancel_callback(self):
"""Process Pool Fork CancelledError is forwarded to callback."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.schedule(long_function)
future.add_done_callback(self.callback)
time.sleep(0.1) # let the process pick up the task
self.assertTrue(future.cancel())
self.event.wait()
self.assertTrue(isinstance(self.exception, CancelledError))
@unittest.skipIf(sys.platform == 'darwin', "Not supported on MAC OS")
def test_process_pool_different_process(self):
"""Process Pool Fork multiple futures are handled by different processes."""
futures = []
with ProcessPool(max_workers=2, context=mp_context) as pool:
for _ in range(0, 5):
futures.append(pool.schedule(pid_function))
self.assertEqual(len(set([f.result() for f in futures])), 2)
def test_process_pool_future_limit(self):
"""Process Pool Fork tasks limit is honored."""
futures = []
with ProcessPool(max_workers=1, max_tasks=2, context=mp_context) as pool:
for _ in range(0, 4):
futures.append(pool.schedule(pid_function))
self.assertEqual(len(set([f.result() for f in futures])), 2)
def test_process_pool_stop_timeout(self):
"""Process Pool Fork workers are stopped if future timeout."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
future1 = pool.schedule(pid_function)
pool.schedule(long_function, timeout=0.1)
future2 = pool.schedule(pid_function)
self.assertNotEqual(future1.result(), future2.result())
def test_process_pool_stop_cancel(self):
"""Process Pool Fork workers are stopped if future is cancelled."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
future1 = pool.schedule(pid_function)
cancel_future = pool.schedule(long_function)
time.sleep(0.1) # let the process pick up the task
cancel_future.cancel()
future2 = pool.schedule(pid_function)
self.assertNotEqual(future1.result(), future2.result())
def test_process_pool_initializer(self):
"""Process Pool Fork initializer is correctly run."""
with ProcessPool(initializer=initializer, initargs=[1], context=mp_context) as pool:
future = pool.schedule(initializer_function)
self.assertEqual(future.result(), 1)
def test_process_pool_broken_initializer(self):
"""Process Pool Fork broken initializer is notified."""
with self.assertRaises(RuntimeError):
with ProcessPool(initializer=broken_initializer, context=mp_context) as pool:
pool.active
time.sleep(0.4)
pool.schedule(function)
def test_process_pool_running(self):
"""Process Pool Fork is active if a future is scheduled."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
pool.schedule(function, args=[1])
self.assertTrue(pool.active)
def test_process_pool_stopped(self):
"""Process Pool Fork is not active once stopped."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
pool.schedule(function, args=[1])
self.assertFalse(pool.active)
def test_process_pool_close_futures(self):
"""Process Pool Fork all futures are performed on close."""
futures = []
pool = ProcessPool(max_workers=1, context=mp_context)
for index in range(10):
futures.append(pool.schedule(function, args=[index]))
pool.close()
pool.join()
map(self.assertTrue, [f.done() for f in futures])
def test_process_pool_close_stopped(self):
"""Process Pool Fork is stopped after close."""
pool = ProcessPool(max_workers=1, context=mp_context)
pool.schedule(function, args=[1])
pool.close()
pool.join()
self.assertFalse(pool.active)
def test_process_pool_stop_futures(self):
"""Process Pool Fork not all futures are performed on stop."""
futures = []
pool = ProcessPool(max_workers=1, context=mp_context)
for index in range(10):
futures.append(pool.schedule(function, args=[index]))
pool.stop()
pool.join()
self.assertTrue(len([f for f in futures if not f.done()]) > 0)
def test_process_pool_stop_stopped(self):
"""Process Pool Fork is stopped after stop."""
pool = ProcessPool(max_workers=1, context=mp_context)
pool.schedule(function, args=[1])
pool.stop()
pool.join()
self.assertFalse(pool.active)
def test_process_pool_stop_stopped_callback(self):
"""Process Pool Fork is stopped in callback."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
def stop_pool_callback(_):
pool.stop()
future = pool.schedule(function, args=[1])
future.add_done_callback(stop_pool_callback)
with self.assertRaises(RuntimeError):
for index in range(10):
time.sleep(0.1)
pool.schedule(long_function, args=[index])
self.assertFalse(pool.active)
def test_process_pool_large_data(self):
"""Process Pool Fork large data is sent on the channel."""
data = "a" * 1098 * 1024 * 100 # 100 Mb
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.schedule(
function, args=[data], kwargs={'keyword_argument': ''})
self.assertEqual(data, future.result())
def test_process_pool_stop_large_data(self):
"""Process Pool Fork is stopped if large data is sent on the channel."""
data = "a" * 1098 * 1024 * 100 # 100 Mb
pool = ProcessPool(max_workers=1, context=mp_context)
pool.schedule(function, args=[data])
time.sleep(1)
pool.stop()
pool.join()
self.assertFalse(pool.active)
def test_process_pool_join_workers(self):
"""Process Pool Fork no worker is running after join."""
pool = ProcessPool(max_workers=4, context=mp_context)
pool.schedule(function, args=[1])
pool.stop()
pool.join()
self.assertEqual(len(pool._pool_manager.worker_manager.workers), 0)
def test_process_pool_join_running(self):
"""Process Pool Fork RuntimeError is raised if active pool joined."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
pool.schedule(function, args=[1])
self.assertRaises(RuntimeError, pool.join)
def test_process_pool_join_futures_timeout(self):
"""Process Pool Fork TimeoutError is raised if join on long futures."""
pool = ProcessPool(max_workers=1, context=mp_context)
for _ in range(2):
pool.schedule(long_function)
pool.close()
self.assertRaises(TimeoutError, pool.join, 0.4)
pool.stop()
pool.join()
def test_process_pool_callback_error(self):
"""Process Pool Fork does not stop if error in callback."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.schedule(function, args=[1],
kwargs={'keyword_argument': 1})
future.add_done_callback(self.callback)
# sleep enough to ensure callback is run
time.sleep(0.1)
pool.schedule(function, args=[1],
kwargs={'keyword_argument': 1})
def test_process_pool_exception_isolated(self):
"""Process Pool Fork an BaseException does not affect other futures."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.schedule(error_function)
try:
future.result()
except BaseException:
pass
future = pool.schedule(function, args=[1],
kwargs={'keyword_argument': 1})
self.assertEqual(future.result(), 2)
@unittest.skipIf(os.name == 'nt', "Test won't run on Windows'.")
def test_process_pool_ignoring_sigterm(self):
"""Process Pool Fork ignored SIGTERM signal are handled on Unix."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.schedule(sigterm_function, timeout=0.2)
with self.assertRaises(TimeoutError):
future.result()
def test_process_pool_expired_worker(self):
"""Process Pool Fork unexpect death of worker raises ProcessExpired."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.schedule(suicide_function)
worker_pid = list(pool._pool_manager.worker_manager.workers)[0]
with self.assertRaises(ProcessExpired) as exc_ctx:
future.result()
self.assertEqual(exc_ctx.exception.exitcode, 1)
self.assertEqual(exc_ctx.exception.pid, worker_pid)
def test_process_pool_map(self):
"""Process Pool Fork map simple."""
elements = [1, 2, 3]
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.map(function, elements)
generator = future.result()
self.assertEqual(list(generator), elements)
def test_process_pool_map_empty(self):
"""Process Pool Fork map no elements."""
elements = []
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.map(function, elements)
generator = future.result()
self.assertEqual(list(generator), elements)
def test_process_pool_map_single(self):
"""Process Pool Fork map one element."""
elements = [0]
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.map(function, elements)
generator = future.result()
self.assertEqual(list(generator), elements)
def test_process_pool_map_multi(self):
"""Process Pool Fork map multiple iterables."""
expected = (2, 4)
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.map(function, (1, 2, 3), (1, 2))
generator = future.result()
self.assertEqual(tuple(generator), expected)
def test_process_pool_map_one_chunk(self):
"""Process Pool Fork map chunksize 1."""
elements = [1, 2, 3]
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.map(function, elements, chunksize=1)
generator = future.result()
self.assertEqual(list(generator), elements)
def test_process_pool_map_zero_chunk(self):
"""Process Pool Fork map chunksize 0."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
with self.assertRaises(ValueError):
pool.map(function, [], chunksize=0)
def test_process_pool_map_timeout(self):
"""Process Pool Fork map with timeout."""
raised = []
elements = [1, 2, 3]
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.map(long_function, elements, timeout=0.1)
generator = future.result()
while True:
try:
next(generator)
except TimeoutError as error:
raised.append(error)
except StopIteration:
break
self.assertTrue(all((isinstance(e, TimeoutError) for e in raised)))
def test_process_pool_map_timeout_chunks(self):
"""Process Pool Fork map timeout is assigned per chunk."""
elements = [0.1]*20
with ProcessPool(max_workers=1, context=mp_context) as pool:
# it takes 1s to process a chunk
future = pool.map(
long_function, elements, chunksize=5, timeout=1.8)
generator = future.result()
self.assertEqual(list(generator), elements)
def test_process_pool_map_error(self):
"""Process Pool Fork errors do not stop the iteration."""
raised = None
elements = [1, 'a', 3]
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.map(function, elements)
generator = future.result()
while True:
try:
result = next(generator)
except TypeError as error:
raised = error
except StopIteration:
break
self.assertEqual(result, 3)
self.assertTrue(isinstance(raised, TypeError))
def test_process_pool_map_cancel(self):
"""Process Pool Fork cancel iteration."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.map(long_function, range(5))
generator = future.result()
self.assertEqual(next(generator), 0)
future.cancel()
for _ in range(4):
with self.assertRaises(CancelledError):
next(generator)
def test_process_pool_map_broken_pool(self):
"""Process Pool Fork Broken Pool."""
elements = [1, 2, 3]
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.map(long_function, elements, timeout=1)
generator = future.result()
pool._context.status = PoolStatus.ERROR
while True:
try:
next(generator)
except TimeoutError as error:
self.assertFalse(pool.active)
future.cancel()
break
except StopIteration:
break
def test_process_pool_child_process(self):
"""Process Pool Fork worker starts process."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.schedule(process_function)
self.assertEqual(future.result(), 1)
def test_process_pool_child_pool(self):
"""Process Pool Fork worker starts multiprocessing.Pool."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.schedule(pool_function)
self.assertEqual(future.result(), 1)
def test_process_pool_child_pebble(self):
"""Process Pool Fork worker starts pebble.ProcessPool."""
with ProcessPool(max_workers=1, context=mp_context) as pool:
future = pool.schedule(pebble_function)
self.assertEqual(future.result(), 1)
@unittest.skipIf(not supported, "Start method is not supported")
class TestAsyncIOProcessPool(unittest.TestCase):
def setUp(self):
self.event = None
self.result = None
self.exception = None
def callback(self, future):
try:
self.result = future.result()
# asyncio.exception.CancelledError does not inherit from BaseException
except BaseException as error:
self.exception = error
finally:
self.event.set()
def test_process_pool_single_future(self):
"""Process Pool Fork single future."""
async def test(pool):
loop = asyncio.get_running_loop()
return await loop.run_in_executor(pool, function, None, 1)
with ProcessPool(max_workers=1, context=mp_context) as pool:
self.assertEqual(asyncio.run(test(pool)), 1)
def test_process_pool_multiple_futures(self):
"""Process Pool Fork multiple futures."""
async def test(pool):
futures = []
loop = asyncio.get_running_loop()
for _ in range(5):
futures.append(loop.run_in_executor(pool, function, None, 1))
return await asyncio.wait(futures)
with ProcessPool(max_workers=2, context=mp_context) as pool:
self.assertEqual(sum(r.result()
for r in asyncio.run(test(pool))[0]), 5)
def test_process_pool_callback(self):
"""Process Pool Fork result is forwarded to the callback."""
async def test(pool):
loop = asyncio.get_running_loop()
self.event = asyncio.Event()
self.event.clear()
future = loop.run_in_executor(pool, function, None, 1)
future.add_done_callback(self.callback)
await self.event.wait()
with ProcessPool(max_workers=1, context=mp_context) as pool:
asyncio.run(test(pool))
self.assertEqual(self.result, 1)
def test_process_pool_error(self):
"""Process Pool Fork errors are raised by future get."""
async def test(pool):
loop = asyncio.get_running_loop()
return await loop.run_in_executor(pool, error_function, None)
with ProcessPool(max_workers=1, context=mp_context) as pool:
with self.assertRaises(BaseException):
asyncio.run(test(pool))
def test_process_pool_error_returned(self):
"""Process Pool Fork returned errors are returned by future get."""
async def test(pool):
loop = asyncio.get_running_loop()
return await loop.run_in_executor(pool, return_error_function, None)
with ProcessPool(max_workers=1, context=mp_context) as pool:
self.assertIsInstance(asyncio.run(test(pool)), BaseException)
def test_process_pool_error_callback(self):
"""Process Pool Fork errors are forwarded to callback."""
async def test(pool):
loop = asyncio.get_running_loop()
self.event = asyncio.Event()
self.event.clear()
future = loop.run_in_executor(pool, error_function, None)
future.add_done_callback(self.callback)
await self.event.wait()
with ProcessPool(max_workers=1, context=mp_context) as pool:
asyncio.run(test(pool))
self.assertTrue(isinstance(self.exception, BaseException))
def test_process_pool_timeout(self):
"""Process Pool Fork future raises TimeoutError if so."""
async def test(pool):
loop = asyncio.get_running_loop()
return await loop.run_in_executor(pool, long_function, 0.1)
with ProcessPool(max_workers=1, context=mp_context) as pool:
with self.assertRaises(asyncio.TimeoutError):
asyncio.run(test(pool))
def test_process_pool_timeout_callback(self):
"""Process Pool Fork TimeoutError is forwarded to callback."""
async def test(pool):
loop = asyncio.get_running_loop()
self.event = asyncio.Event()
self.event.clear()
future = loop.run_in_executor(pool, long_function, 0.1)
future.add_done_callback(self.callback)
await asyncio.sleep(0.1) # let the process pick up the task
await self.event.wait()
with ProcessPool(max_workers=1, context=mp_context) as pool:
asyncio.run(test(pool))
self.assertTrue(isinstance(self.exception, asyncio.TimeoutError))
def test_process_pool_cancel(self):
"""Process Pool Fork future raises CancelledError if so."""
async def test(pool):
loop = asyncio.get_running_loop()
future = loop.run_in_executor(pool, long_function, None)
await asyncio.sleep(0.1) # let the process pick up the task
self.assertTrue(future.cancel())
return await future
with ProcessPool(max_workers=1, context=mp_context) as pool:
with self.assertRaises(asyncio.CancelledError):
asyncio.run(test(pool))
def test_process_pool_cancel_callback(self):
"""Process Pool Fork CancelledError is forwarded to callback."""
async def test(pool):
loop = asyncio.get_running_loop()
self.event = asyncio.Event()
self.event.clear()
future = loop.run_in_executor(pool, long_function, None)
future.add_done_callback(self.callback)
await asyncio.sleep(0.1) # let the process pick up the task
self.assertTrue(future.cancel())
await self.event.wait()
with ProcessPool(max_workers=1, context=mp_context) as pool:
asyncio.run(test(pool))
self.assertTrue(isinstance(self.exception, asyncio.CancelledError))
def test_process_pool_stop_timeout(self):
"""Process Pool Fork workers are stopped if future timeout."""
async def test(pool):
loop = asyncio.get_running_loop()
future1 = loop.run_in_executor(pool, pid_function, None)
with self.assertRaises(asyncio.TimeoutError):
await loop.run_in_executor(pool, long_function, 0.1)
future2 = loop.run_in_executor(pool, pid_function, None)
self.assertNotEqual(await future1, await future2)
with ProcessPool(max_workers=1, context=mp_context) as pool:
asyncio.run(test(pool))
def test_process_pool_stop_cancel(self):
"""Process Pool Fork workers are stopped if future is cancelled."""
async def test(pool):
loop = asyncio.get_running_loop()
future1 = loop.run_in_executor(pool, pid_function, None)
cancel_future = loop.run_in_executor(pool, long_function, None)
await asyncio.sleep(0.1) # let the process pick up the task
self.assertTrue(cancel_future.cancel())
future2 = loop.run_in_executor(pool, pid_function, None)
self.assertNotEqual(await future1, await future2)
with ProcessPool(max_workers=1, context=mp_context) as pool:
asyncio.run(test(pool))
# DEADLOCK TESTS
def broken_worker_process_tasks(_, channel):
"""Process failing in receiving new tasks."""
with channel.mutex.reader:
os._exit(1)
def broken_worker_process_result(_, channel):
"""Process failing in delivering result."""
try:
for _ in pebble.pool.process.worker_get_next_task(channel, 2):
with channel.mutex.writer:
os._exit(1)
except OSError:
os._exit(1)
@unittest.skipIf(not supported, "Start method is not supported")
class TestProcessPoolDeadlockOnNewFutures(unittest.TestCase):
def setUp(self):
self.worker_process = pebble.pool.process.worker_process
pebble.pool.process.worker_process = broken_worker_process_tasks
pebble.CONSTS.channel_lock_timeout = 0.1
def tearDown(self):
pebble.pool.process.worker_process = self.worker_process
pebble.CONSTS.channel_lock_timeout = 60
def test_pool_deadlock_stop(self):
"""Process Pool Fork reading deadlocks are stopping the Pool."""
with self.assertRaises(RuntimeError):
pool = pebble.ProcessPool(max_workers=1, context=mp_context)
for _ in range(10):
pool.schedule(function)
time.sleep(0.2)
@unittest.skipIf(not supported, "Start method is not supported")
class TestProcessPoolDeadlockOnResult(unittest.TestCase):
def setUp(self):
self.worker_process = pebble.pool.process.worker_process
pebble.pool.process.worker_process = broken_worker_process_result
pebble.CONSTS.channel_lock_timeout = 0.1
def tearDown(self):
pebble.pool.process.worker_process = self.worker_process
pebble.CONSTS.channel_lock_timeout = 60
def test_pool_deadlock(self):
"""Process Pool Fork no deadlock if writing worker dies locking channel."""
with pebble.ProcessPool(max_workers=1, context=mp_context) as pool:
with self.assertRaises(pebble.ProcessExpired):
pool.schedule(function).result()
@unittest.skipIf(not supported, "Start method is not supported")
class TestProcessPoolDeadlockOnCancelLargeData(unittest.TestCase):
def test_pool_deadlock_stop_cancel(self):
"""Pool is stopped when futures are cancelled on large data."""
data = b'A' * 1024 * 1024 * 100
with pebble.ProcessPool() as pool:
futures = [pool.schedule(function, args=[data]) for _ in range(10)]
concurrent.futures.wait(
futures,
return_when=concurrent.futures.FIRST_COMPLETED
)
for f in futures:
f.cancel()
pool.stop()
|