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

Add LocalEventManager #40

Merged
merged 7 commits into from
Feb 15, 2024
Merged

Add LocalEventManager #40

merged 7 commits into from
Feb 15, 2024

Conversation

vdusek
Copy link
Collaborator

@vdusek vdusek commented Feb 1, 2024

Ticket

Description

  • Rewrote of LocalEventManager from https://github.com/apify/crawlee/blob/v3.7.3/packages/core/src/events/local_event_manager.ts.
    • Basically, it just schedules a periodic task for retrieving the CPU and memory-related data.
  • I implemented the helper class RecurringTask as an alternative to betterSetInterval from @apify/utilities.
  • CPU and memory data are retrieved using the psutil library.
  • I put the default threshold values into Config, as it is done in TS implementation since they are probably going to be used in multiple places.

Test script

import asyncio
import logging
from datetime import timedelta

from crawlee import Config
from crawlee.events import LocalEventManager
from crawlee.events.types import Event, EventSystemInfoData
from crawlee.log_config import CrawleeLogFormatter

# Configuration of the root logger
handler = logging.StreamHandler()
handler.setFormatter(CrawleeLogFormatter(include_logger_name=True))
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)
root_logger.addHandler(handler)

logger = logging.getLogger(__name__)


def system_info_handler(data: EventSystemInfoData) -> None:
    logger.info(f'data={data}')


async def main() -> None:
    logger.info('Starting the main...')
    config = Config(system_info_interval=timedelta(seconds=0.5))

    event_manager = LocalEventManager(config=config)
    await event_manager.init()
    event_manager.on(event=Event.SYSTEM_INFO, listener=system_info_handler)

    # Alocating some memory...
    await asyncio.sleep(1)
    a = list(range(1_000_000))
    await asyncio.sleep(1)
    b = list(range(1_000_000))
    await asyncio.sleep(1)
    c = list(range(1_000_000))

    logger.info('Waiting for all listeners to complete...')
    await event_manager.close(timeout=timedelta(seconds=1))
    logger.info('Done waiting for all listeners to complete.')


if __name__ == '__main__':
    asyncio.run(main())

@vdusek vdusek force-pushed the add-local-event-manager branch from 7ce8ee8 to c0ca5b3 Compare February 12, 2024 16:56
@github-actions github-actions bot added this to the 83rd sprint - Tooling team milestone Feb 12, 2024
@github-actions github-actions bot added the t-tooling Issues with this label are in the ownership of the tooling team. label Feb 12, 2024
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

⚠️ Pull Request Tookit has failed!

Pull request is neither linked to an issue or epic nor labeled as adhoc!

@vdusek vdusek force-pushed the add-local-event-manager branch 2 times, most recently from 8ab7158 to 35d1be1 Compare February 13, 2024 13:57
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

⚠️ Pull Request Tookit has failed!

Pull request is neither linked to an issue or epic nor labeled as adhoc!

@vdusek vdusek marked this pull request as ready for review February 13, 2024 14:05
@vdusek vdusek removed the t-tooling Issues with this label are in the ownership of the tooling team. label Feb 13, 2024
@vdusek vdusek force-pushed the add-local-event-manager branch from 35d1be1 to 57762d4 Compare February 13, 2024 14:06
@github-actions github-actions bot added the t-tooling Issues with this label are in the ownership of the tooling team. label Feb 13, 2024
@vdusek vdusek force-pushed the add-local-event-manager branch 2 times, most recently from 694f859 to e6ca8db Compare February 13, 2024 15:19
@vdusek vdusek changed the title [WIP] Add LocalEventManager Add LocalEventManager Feb 13, 2024
@vdusek vdusek requested a review from janbuchar February 13, 2024 15:27
src/crawlee/config.py Show resolved Hide resolved
src/crawlee/autoscaling/types.py Show resolved Hide resolved
src/crawlee/_utils/recurring_task.py Outdated Show resolved Hide resolved
src/crawlee/events/local_event_manager.py Outdated Show resolved Hide resolved
src/crawlee/events/local_event_manager.py Outdated Show resolved Hide resolved
src/crawlee/events/local_event_manager.py Show resolved Hide resolved
)

def _get_cpu_info(self: LocalEventManager) -> LoadRatioInfo:
cpu_actual_ratio = psutil.cpu_percent() / 100
Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. an explicit interval sounds like a good idea for cpu_percent
  2. this won't include CPU usage of e.g. playwright-controlled browsers, right? Shouldn't we check child processes as well, like in the memory usage method?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

1 - sure thing, makes sense

2 - If I understand the doc of psutil correctly ... The psutil.cpu_percent function measures the system-wide CPU utilization, not just the CPU usage by your specific process or its child processes. So there should be no need to check child processes. I'd say in this case, it's what we want.

@janbuchar
Copy link
Collaborator

Oh, and could we write some tests?

@vdusek vdusek removed the t-tooling Issues with this label are in the ownership of the tooling team. label Feb 14, 2024
@vdusek vdusek force-pushed the add-local-event-manager branch from e6ca8db to b5ca2a6 Compare February 14, 2024 12:21
@github-actions github-actions bot added the t-tooling Issues with this label are in the ownership of the tooling team. label Feb 14, 2024
@vdusek vdusek force-pushed the add-local-event-manager branch from a2a0cca to 188a028 Compare February 14, 2024 14:15
@vdusek vdusek requested a review from janbuchar February 14, 2024 14:20
@vdusek
Copy link
Collaborator Author

vdusek commented Feb 14, 2024

Oh, and could we write some tests?

Of course, as discussed previously, I'll prepare tests once the core components stabilize.

The recurring task, event manager, and local event manager can be considered in a solid state, I'll provide tests for them in the following PRs.

I will provide tests for the system status module later since I suppose there could be some changes when finishing the Snapshotter.

@vdusek
Copy link
Collaborator Author

vdusek commented Feb 15, 2024

Since Honza is sick, I am merging this, so that I can work on subsequent things. In case of further comments, I'll make another contribution.

@vdusek vdusek merged commit 314ac93 into master Feb 15, 2024
19 checks passed
@vdusek vdusek deleted the add-local-event-manager branch February 15, 2024 10:20
@vdusek vdusek removed the t-tooling Issues with this label are in the ownership of the tooling team. label Feb 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants