code: def test_write_preferred_over_read():
      in_read_event = threading.Event()
      block_read_event = threading.Event()
      in_other_read_event = threading.Event()
      block_other_read_event = threading.Event()
      in_write_event = threading.Event()
      block_write_event = threading.Event()
  
      lock = ReadWriteLock()
  
      reader = threading.Thread(
          target=read, args=(lock, in_read_event, block_read_event)
      )
      other_reader = threading.Thread(
          target=read,
          args=(lock, in_other_read_event, block_other_read_event),
      )
      writer = threading.Thread(
          target=write, args=(lock, in_write_event, block_write_event)
      )
  
      try:
          reader.start()
          in_read_event.wait()
  
          writer.start()
          result = in_write_event.wait(0.1)
          # The write is blocked by the read lock
          assert result is False
          assert lock._write_pending is True
  
          other_reader.start()
          result = in_other_read_event.wait(0.1)
          # The read is blocked by the pending write
          assert result is False
  
          block_read_event.set()
          reader.join()
  
          # The write happens next
          in_write_event.wait()
          assert in_other_read_event.is_set() is False
  
          block_write_event.set()
          writer.join()
  
          in_other_read_event.wait()
          block_other_read_event.set()
          other_reader.join()
      finally:
          block_read_event.set()
          block_write_event.set()
          block_other_read_event.set()
          reader.join()
          writer.join()
          other_reader.join()
  
file_path: distributed/distributed/shuffle/tests/test_read_write_lock.py
imports: from __future__ import annotations
  import threading
  import pytest
  from distributed.shuffle._disk import ReadWriteLock
label_desc: Use image node_id 9 for calling a global function with example usage: test_write_preferred_over_read() without return types