Не могу добавить таску в apscheduler

error:

TypeError: cannot pickle 'weakref.ReferenceType' object
Traceback (most recent call last):
  File "C:\Python_all_projects\Ordered TG Bots\Shrink2\.venv\Lib\site-packages\aiogram\dispatcher\dispatcher.py", line 309, in _process_update
    response = await self.feed_update(bot, update, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python_all_projects\Ordered TG Bots\Shrink2\.venv\Lib\site-packages\aiogram\dispatcher\dispatcher.py", line 158, in feed_update
    response = await self.update.wrap_outer_middleware(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python_all_projects\Ordered TG Bots\Shrink2\.venv\Lib\site-packages\aiogram\dispatcher\middlewares\error.py", line 25, in __call__
    return await handler(event, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python_all_projects\Ordered TG Bots\Shrink2\.venv\Lib\site-packages\aiogram\dispatcher\middlewares\user_context.py", line 49, in __call__
    return await handler(event, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python_all_projects\Ordered TG Bots\Shrink2\.venv\Lib\site-packages\aiogram\fsm\middleware.py", line 42, in __call__
    return await handler(event, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python_all_projects\Ordered TG Bots\Shrink2\.venv\Lib\site-packages\aiogram\dispatcher\event\telegram.py", line 121, in trigger
    return await wrapped_inner(event, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python_all_projects\Ordered TG Bots\Shrink2\.venv\Lib\site-packages\dishka\integrations\aiogram.py", line 43, in __call__     
    return await handler(event, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python_all_projects\Ordered TG Bots\Shrink2\.venv\Lib\site-packages\aiogram\dispatcher\event\handler.py", line 43, in call    
    return await wrapped()
           ^^^^^^^^^^^^^^^
  File "C:\Python_all_projects\Ordered TG Bots\Shrink2\.venv\Lib\site-packages\aiogram\dispatcher\dispatcher.py", line 276, in _listen_update
    return await self.propagate_event(update_type=update_type, event=event, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python_all_projects\Ordered TG Bots\Shrink2\.venv\Lib\site-packages\aiogram\dispatcher\router.py", line 142, in propagate_event
    return await observer.wrap_outer_middleware(_wrapped, event=event, data=kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python_all_projects\Ordered TG Bots\Shrink2\.venv\Lib\site-packages\aiogram\dispatcher\router.py", line 137, in _wrapped      
    return await self._propagate_event(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python_all_projects\Ordered TG Bots\Shrink2\.venv\Lib\site-packages\aiogram\dispatcher\router.py", line 170, in _propagate_event
    response = await router.propagate_event(update_type=update_type, event=event, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python_all_projects\Ordered TG Bots\Shrink2\.venv\Lib\site-packages\aiogram\dispatcher\router.py", line 142, in propagate_event
    return await observer.wrap_outer_middleware(_wrapped, event=event, data=kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python_all_projects\Ordered TG Bots\Shrink2\.venv\Lib\site-packages\aiogram\dispatcher\router.py", line 137, in _wrapped      
    return await self._propagate_event(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python_all_projects\Ordered TG Bots\Shrink2\.venv\Lib\site-packages\aiogram\dispatcher\router.py", line 162, in _propagate_event
    response = await observer.trigger(event, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python_all_projects\Ordered TG Bots\Shrink2\.venv\Lib\site-packages\aiogram\dispatcher\event\telegram.py", line 121, in trigger
    return await wrapped_inner(event, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python_all_projects\Ordered TG Bots\Shrink2\.venv\Lib\site-packages\dishka\integrations\aiogram.py", line 43, in __call__     
    return await handler(event, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python_all_projects\Ordered TG Bots\Shrink2\.venv\Lib\site-packages\aiogram\dispatcher\event\handler.py", line 43, in call    
    return await wrapped()
           ^^^^^^^^^^^^^^^
  File "C:\Python_all_projects\Ordered TG Bots\Shrink2\.venv\Lib\site-packages\dishka\integrations\base.py", line 90, in autoinjected_func
    return await func(*args, **kwargs, **solved)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python_all_projects\Ordered TG Bots\Shrink2\shrink\app\bot\callbacks\callbacks.py", line 466, in turn_on_mailing_call
    scheduler.add_job(
  File "C:\Python_all_projects\Ordered TG Bots\Shrink2\.venv\Lib\site-packages\apscheduler_di\decorator.py", line 159, in add_job        
    self._scheduler._real_add_job(job, jobstore, replace_existing)
  File "C:\Python_all_projects\Ordered TG Bots\Shrink2\.venv\Lib\site-packages\apscheduler\schedulers\base.py", line 881, in _real_add_job
    store.add_job(job)
  File "C:\Python_all_projects\Ordered TG Bots\Shrink2\.venv\Lib\site-packages\apscheduler\jobstores\redis.py", line 81, in add_job      
    pipe.hset(self.jobs_key, job.id, pickle.dumps(job.__getstate__(),
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: cannot pickle 'weakref.ReferenceType' object

code:

scheduler.add_job(
                func=mailing_manager.mailing,
                trigger="cron",
                day=f"*/{settings.frequency}",
                hour=schedule_time.hour,
                minute=schedule_time.minute,
                kwargs={'user_id': user_id, 'bot': bot,}, 
                id=str(user_id)
            )

class AutoMailingManager:

class AutoMailingManager():
    def __init__(
        self,
        extra_mailing: ExtraMailing,
        email_service: EmailService,
        settings_service: SettingsService,
        user_service: UserService,
        email_dal: UserEmailDAL,
        audio_dal: UserAudioDAL,
    ) -> None:
        self._client = SMTP(hostname='smtp.gmail.com', port=587)
        self._email = MIMEMultipart()
        self._extra_mailing = extra_mailing
        self._email_service = email_service
        self._settings_service = settings_service
        self._email_dal = email_dal
        self._audio_dal = audio_dal
        self._user_service = user_service

    async def mailing(self, user_id: int, bot: Bot) -> None:
              pass

В чем проблема, как это решить?


Ответы (1 шт):

Автор решения: sinn

В общем я решил эту проблему. Она заключалась в том, что я в кварги передавал бота, а нужно было только данные

→ Ссылка