3mora2 / WPP_Whatsapp

WPP_Whatsapp aim of exporting functions from WhatsApp Web to the python, which can be used to support the creation of any interaction, such as customer service, media sending, intelligence recognition based on phrases artificial and many other things, use your imagination
MIT License
75 stars 12 forks source link

The usage of close() is causing an error. #35

Closed Jeck-Liu-Create closed 6 months ago

Jeck-Liu-Create commented 7 months ago

I've defined a SetWhatsApp class that contains two methods:

 async def create_web_send(self, headless=False):
        if headless:
            self.creator = Create(session=self.session_name, browser="firefox", headless=headless)
        else:
            self.creator = Create(session=self.session_name, browser="firefox")
        self.client = self.creator.start()
        self.browser_open = True

  def creator_close(self):
      logger.info("close")
      try:
          self.creator.close()
      except Exception as e:
          print(e)

In the main program, I implemented the start and restart functions:

self.send_app = SetWhatsApp(session_name='client')

def start_browser(self):
    if not self.send_app.check_state():
        self.check_send_app_starting()

        async def run_async():
            await self.send_app.create_web_send(headless=self.headless)
            if self.send_app.check_state():
                self.send_start.config(text="Connecting中", state="disabled") 
                self.start_send_app_timer()  # Call start_send_app_timer after successful connection
            else:
                self.send_start.config(text="Start Button", state="normal") 

        thread = threading.Thread(target=lambda: asyncio.run(run_async()))
        thread.start()
    else:
        self.stop_send_app_timer()

        self.send_app.creator_close()

        self.check_send_app_restate()

        async def run_async():
            await self.send_app.create_web_send(headless=self.headless)
            if self.send_app.check_state():
                self.send_start.config(text="Connecting", state="disabled")
                self.start_send_app_timer()  # Call start_send_app_timer after successful connection
            else:
                self.send_start.config(text="Start Button", state="normal") 
            print("Restart Browse") 

        thread = threading.Thread(target=lambda: asyncio.run(run_async()))
        thread.start()

When I restart, it throws an error shortly afterward, but it does not affect the browser's ability to continue functioning after the restart.

Traceback (most recent call last):
  File "C:\Users\Jeck\anaconda3\envs\LeboncoinMeaasge\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\Jeck\anaconda3\envs\LeboncoinMeaasge\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Jeck\PycharmProjects\watermarking-app\WPP_Whatsapp\controllers\initializer.py", line 82, in _onStateChange
    connected = self.ThreadsafeBrowser.sync_page_evaluate("() => WPP.conn.isRegistered()")
  File "C:\Users\Jeck\PycharmProjects\watermarking-app\WPP_Whatsapp\PlaywrightSafeThread\browser\threadsafe_browser.py", line 353, in sync_page_evaluate
    return self.run_threadsafe(self.page.evaluate, expression, arg, timeout_=timeout_)
  File "C:\Users\Jeck\PycharmProjects\watermarking-app\WPP_Whatsapp\PlaywrightSafeThread\browser\threadsafe_browser.py", line 404, in run_threadsafe
    result = future.result(timeout=timeout_)
  File "C:\Users\Jeck\anaconda3\envs\LeboncoinMeaasge\lib\concurrent\futures\_base.py", line 446, in result
    raise TimeoutError()
concurrent.futures._base.TimeoutError
Exception in thread Thread-13:
Traceback (most recent call last):
  File "C:\Users\Jeck\anaconda3\envs\LeboncoinMeaasge\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\Jeck\anaconda3\envs\LeboncoinMeaasge\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Jeck\PycharmProjects\watermarking-app\WPP_Whatsapp\controllers\initializer.py", line 82, in _onStateChange
    connected = self.ThreadsafeBrowser.sync_page_evaluate("() => WPP.conn.isRegistered()")
  File "C:\Users\Jeck\PycharmProjects\watermarking-app\WPP_Whatsapp\PlaywrightSafeThread\browser\threadsafe_browser.py", line 353, in sync_page_evaluate
    return self.run_threadsafe(self.page.evaluate, expression, arg, timeout_=timeout_)
  File "C:\Users\Jeck\PycharmProjects\watermarking-app\WPP_Whatsapp\PlaywrightSafeThread\browser\threadsafe_browser.py", line 404, in run_threadsafe
    result = future.result(timeout=timeout_)
  File "C:\Users\Jeck\anaconda3\envs\LeboncoinMeaasge\lib\concurrent\futures\_base.py", line 446, in result
    raise TimeoutError()
concurrent.futures._base.TimeoutError
Exception in thread Exception in thread Thread-15:
Traceback (most recent call last):
  File "C:\Users\Jeck\anaconda3\envs\LeboncoinMeaasge\lib\threading.py", line 932, in _bootstrap_inner
Thread-14:
Traceback (most recent call last):
  File "C:\Users\Jeck\anaconda3\envs\LeboncoinMeaasge\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\Jeck\anaconda3\envs\LeboncoinMeaasge\lib\threading.py", line 870, in run
    self.run()
  File "C:\Users\Jeck\anaconda3\envs\LeboncoinMeaasge\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Jeck\PycharmProjects\watermarking-app\WPP_Whatsapp\controllers\initializer.py", line 82, in _onStateChange
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Jeck\PycharmProjects\watermarking-app\WPP_Whatsapp\controllers\initializer.py", line 82, in _onStateChange
    connected = self.ThreadsafeBrowser.sync_page_evaluate("() => WPP.conn.isRegistered()")
  File "C:\Users\Jeck\PycharmProjects\watermarking-app\WPP_Whatsapp\PlaywrightSafeThread\browser\threadsafe_browser.py", line 353, in sync_page_evaluate
    connected = self.ThreadsafeBrowser.sync_page_evaluate("() => WPP.conn.isRegistered()")
  File "C:\Users\Jeck\PycharmProjects\watermarking-app\WPP_Whatsapp\PlaywrightSafeThread\browser\threadsafe_browser.py", line 353, in sync_page_evaluate
    return self.run_threadsafe(self.page.evaluate, expression, arg, timeout_=timeout_)
  File "C:\Users\Jeck\PycharmProjects\watermarking-app\WPP_Whatsapp\PlaywrightSafeThread\browser\threadsafe_browser.py", line 404, in run_threadsafe
    return self.run_threadsafe(self.page.evaluate, expression, arg, timeout_=timeout_)
  File "C:\Users\Jeck\PycharmProjects\watermarking-app\WPP_Whatsapp\PlaywrightSafeThread\browser\threadsafe_browser.py", line 404, in run_threadsafe
    result = future.result(timeout=timeout_)
    result = future.result(timeout=timeout_)
  File "C:\Users\Jeck\anaconda3\envs\LeboncoinMeaasge\lib\concurrent\futures\_base.py", line 446, in result
  File "C:\Users\Jeck\anaconda3\envs\LeboncoinMeaasge\lib\concurrent\futures\_base.py", line 446, in result
    raise TimeoutError()
concurrent.futures._base.TimeoutError
    raise TimeoutError()
concurrent.futures._base.TimeoutError

While executing self.creator.close(), a warning was logged:

2024-02-28 16:07:37,238 - py.warnings - WARNING - C:\Users\Jeck\PycharmProjects\watermarking-app\sendmain.py:209: RuntimeWarning: coroutine 'Create.close' was never awaited
  self.creator.close()

This warning indicates that the Create.close coroutine was called but not awaited, which is required for coroutine functions in Python's asyncio library. To fix this warning, you need to await the coroutine:

self.creator.close()

After this change, you would also need to ensure that creator_close is called within an asynchronous context.

Jeck-Liu-Create commented 7 months ago

I referred to Session[session].close() for the direct use of close(). It can close the browser quickly, but it may cause other side effects.

https://github.com/3mora2/WPP_Whatsapp/blob/a593a8436ed70e49940bdbb649d342106ea3d9b3/examples/multi_session.py#L16

3mora2 commented 7 months ago

Use sync_close Will update examples