Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve concurrency on Windows #286

Merged
merged 9 commits into from
Jul 3, 2024
Merged

Improve concurrency on Windows #286

merged 9 commits into from
Jul 3, 2024

Conversation

elacuesta
Copy link
Member

@elacuesta elacuesta commented Jul 2, 2024

Closes #282

Alternative approach to #285

This yields full concurrency ('playwright/page_count/max_concurrent': 32 in stats) with the sample spider from #285:

import scrapy

class DelayTest(scrapy.Spider):
    name = "delay"
    custom_settings = {
        "TWISTED_REACTOR": "twisted.internet.asyncioreactor.AsyncioSelectorReactor",
        "DOWNLOAD_HANDLERS": {
            # "http": "scrapy_playwright.handler.ScrapyPlaywrightDownloadHandler",
            "https": "scrapy_playwright.handler.ScrapyPlaywrightDownloadHandler",
        },
        "PLAYWRIGHT_MAX_PAGES_PER_CONTEXT": 32,
        "CONCURRENT_REQUESTS_PER_IP": 32,
        "CONCURRENT_REQUESTS_PER_DOMAIN": 32,
        "CONCURRENT_REQUESTS": 32,
        "PLAYWRIGHT_LAUNCH_OPTIONS": {"headless": False},
    }

    def start_requests(self):
        for i in range(32):
            yield scrapy.Request(
                url=f"https://httpbin.org/delay/1?i={i}",
                meta={"playwright": True},
            )

    def parse(self, response):
        print(response.url)

@elacuesta elacuesta marked this pull request as ready for review July 2, 2024 18:21
Copy link

codecov bot commented Jul 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (55fd416) to head (9942e6b).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #286   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            6         6           
  Lines          517       543   +26     
=========================================
+ Hits           517       543   +26     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@elacuesta elacuesta added Windows bug Something isn't working labels Jul 3, 2024
async def _process_queue(cls) -> None:
while not cls._stop_event.is_set():
coro, future = await cls._coro_queue.get()
asyncio.create_task(cls._handle_coro(coro, future))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may not apply here but, for the record, when implementing session management for scrapy-zyte-api, I found that I needed to keep live references to task objects until finished to prevent them from being garbage-collected.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, thank you very much for the reference. I haven't seen that in my tests but it might be that my runs were not long enough. I won't include it right away just to keep things simple but I'll keep it in mind in case someone reports it as an issue.

"""Utility class to redirect coroutines to an asyncio event loop running
in a different thread. This allows to use a ProactorEventLoop, which is
supported by Playwright on Windows.
class _ThreadedLoopAdapter:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn’t this still have Windows, Proactor or something of the sort in the name? (I’m looking at def start).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed it like this because it's technically possible to use it on other platforms as well. The concurrency issue also happens on Linux with this threaded loop approach, it's not an intrinsic Windows issue, so I did most of the development in Linux and only then I tried it on Windows to make sure it works.

@elacuesta elacuesta added this to the v0.0.37 milestone Jul 3, 2024
@elacuesta elacuesta merged commit 164ff9a into main Jul 3, 2024
16 checks passed
@elacuesta elacuesta deleted the windows-concurrency-queue branch July 3, 2024 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Windows
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Why can't my Playwright run concurrently?
2 participants