MCPcopy Create free account
hub / github.com/apache/arrow / test_cancellation

Method test_cancellation

python/pyarrow/tests/test_csv.py:1564–1628  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1562
1563 @pytest.mark.threading
1564 def test_cancellation(self):
1565 if (threading.current_thread().ident !=
1566 threading.main_thread().ident):
1567 pytest.skip("test only works from main Python thread")
1568
1569 def signal_from_thread():
1570 # Give our workload a chance to start up
1571 time.sleep(0.2)
1572 signal.raise_signal(signal.SIGINT)
1573
1574 # We start with a small CSV reading workload and increase its size
1575 # until it's large enough to get an interruption during it, even in
1576 # release mode on fast machines.
1577 last_duration = 0.0
1578 workload_size = 100_000
1579 attempts = 0
1580
1581 while last_duration < 5.0 and attempts < 10:
1582 print("workload size:", workload_size)
1583 large_csv = b"a,b,c\n" + b"1,2,3\n" * workload_size
1584 exc_info = None
1585
1586 try:
1587 # We use a signal fd to reliably ensure that the signal
1588 # has been delivered to Python, regardless of how exactly
1589 # it was caught.
1590 with util.signal_wakeup_fd() as sigfd:
1591 try:
1592 t = threading.Thread(target=signal_from_thread)
1593 t.start()
1594 t1 = time.time()
1595 try:
1596 self.read_bytes(large_csv)
1597 except KeyboardInterrupt as e:
1598 exc_info = e
1599 last_duration = time.time() - t1
1600 finally:
1601 # Wait for signal to arrive if it didn't already,
1602 # to avoid getting a KeyboardInterrupt after the
1603 # `except` block below.
1604 select.select([sigfd], [], [sigfd], 10.0)
1605
1606 except KeyboardInterrupt:
1607 # KeyboardInterrupt didn't interrupt `read_bytes` above.
1608 pass
1609
1610 if exc_info is not None:
1611 # We managed to get `self.read_bytes` interrupted, see if it
1612 # was actually interrupted inside Arrow C++ or in the Python
1613 # scaffolding.
1614 if exc_info.__context__ is not None:
1615 # Interrupted inside Arrow C++, we're satisfied now
1616 break
1617
1618 # Increase workload size to get a better chance
1619 workload_size = workload_size * 3
1620
1621 if exc_info is None:

Callers

nothing calls this directly

Calls 5

read_bytesMethod · 0.95
timeMethod · 0.80
startMethod · 0.45
selectMethod · 0.45
failMethod · 0.45

Tested by

no test coverage detected