0

I had this question while adding an explicit wait for selenium as below.

class Wait:
    @staticmethod
    def wait_until_element_present(driver: webdriver, timeout: int = 10) -> None:
        WebDriverWait(driver, timeout).until(
            EC.presence_of_element_located((By.ID, "myDynamicElement"))
        )

Just in case you have not worked with selenium, the Above code simply polls the DOM until the element is present and lets the program moves on. So this method can hold the thread until the condition given evaluates to true. If I want to run tests in parallel, this method might get called simultaneously. My question is, will this delay the other calls to the methods in a parallel test execution situation?

Dorian Turba
  • 3,260
  • 3
  • 23
  • 67
Rasika
  • 312
  • 1
  • 7
  • 19
  • 1
    No. `@staticmethod` alone will not cause blocking as if it was using a lock/semaphore. – dskrypa Feb 11 '23 at 23:45
  • If you want to block other threads, you would need a [Lock](https://docs.python.org/3/library/threading.html#lock-objects). Multiprocessing locks [are also available](https://docs.python.org/3/library/multiprocessing.html#synchronization-primitives). – dskrypa Feb 11 '23 at 23:51
  • Cos I thought static methods service methods calls sequentially ?? – Rasika Feb 12 '23 at 01:28
  • Static methods are essentially just standalone functions in the namespace of a class. You can learn more here: https://stackoverflow.com/q/136097/19070573 – dskrypa Feb 12 '23 at 02:26

1 Answers1

2

A short answer is, it depends on the implementation of that static method. You are misunderstanding the model of executing code.

The body of any function(including static methods and non-static methods) is stored in memory only once as a single object, but there can exist multiple calls of the same function and they can be executed on multiple threads in parallel.

In your case, when multiple threads call the wait_until_element_present() at the same time, the call on one thread will not block another thread if the driver arguments are all different.

relent95
  • 3,703
  • 1
  • 14
  • 17