From 341a25d2f870c3e681ce4f0cc85ee275d77f9ddc Mon Sep 17 00:00:00 2001 From: Bar Harel Date: Mon, 11 Jan 2021 01:21:21 +0200 Subject: Allow Job to be weak-referenced (#470) --- apscheduler/job.py | 2 +- tests/test_job.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/apscheduler/job.py b/apscheduler/job.py index 5c1b75d..7d609fa 100644 --- a/apscheduler/job.py +++ b/apscheduler/job.py @@ -40,7 +40,7 @@ class Job(object): __slots__ = ('_scheduler', '_jobstore_alias', 'id', 'trigger', 'executor', 'func', 'func_ref', 'args', 'kwargs', 'name', 'misfire_grace_time', 'coalesce', 'max_instances', - 'next_run_time') + 'next_run_time', '__weakref__') def __init__(self, scheduler, id=None, **kwargs): super(Job, self).__init__() diff --git a/tests/test_job.py b/tests/test_job.py index 9dc9210..9405554 100644 --- a/tests/test_job.py +++ b/tests/test_job.py @@ -1,4 +1,6 @@ # coding: utf-8 +import gc +import weakref from datetime import datetime, timedelta from functools import partial @@ -64,6 +66,14 @@ def test_remove(job): job._scheduler.remove_job.assert_called_once_with(job.id, None) +def test_weakref(create_job): + job = create_job(func=dummyfunc) + ref = weakref.ref(job) + del job + gc.collect() + assert ref() is None + + def test_pending(job): """ Tests that the "pending" property return True when _jobstore_alias is a string, ``False`` -- cgit v1.2.1