pytorch-lightning

Форк
0
60 строк · 2.2 Кб
1
# Copyright The Lightning AI team.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
6
#
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14

15
import threading
16
from datetime import datetime
17
from typing import Optional
18

19
from croniter import croniter
20
from deepdiff import Delta
21

22
from lightning.app.utilities.proxies import ComponentDelta
23

24

25
class SchedulerThread(threading.Thread):
26
    # TODO (tchaton) Abstract this logic to a generic scheduling service.
27

28
    def __init__(self, app) -> None:
29
        super().__init__(daemon=True)
30
        self._exit_event = threading.Event()
31
        self._sleep_time = 1.0
32
        self._app = app
33

34
    def run(self) -> None:
35
        while not self._exit_event.is_set():
36
            self._exit_event.wait(self._sleep_time)
37
            self.run_once()
38

39
    def run_once(self):
40
        for call_hash in list(self._app._schedules.keys()):
41
            metadata = self._app._schedules[call_hash]
42
            start_time = datetime.fromisoformat(metadata["start_time"])
43
            current_date = datetime.now()
44
            next_event = croniter(metadata["cron_pattern"], start_time).get_next(datetime)
45
            # When the event is reached, send a delta to activate scheduling.
46
            if current_date > next_event:
47
                component_delta = ComponentDelta(
48
                    id=metadata["name"],
49
                    delta=Delta({
50
                        "values_changed": {
51
                            f"root['calls']['scheduling']['{call_hash}']['running']": {"new_value": True}
52
                        }
53
                    }),
54
                )
55
                self._app.delta_queue.put(component_delta)
56
                metadata["start_time"] = next_event.isoformat()
57

58
    def join(self, timeout: Optional[float] = None) -> None:
59
        self._exit_event.set()
60
        super().join(timeout)
61

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.