本文整理汇总了Python中pq.Queue类的典型用法代码示例。如果您正苦于以下问题:Python Queue类的具体用法?Python Queue怎么用?Python Queue使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Queue类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestEnqueueNext
class TestEnqueueNext(TransactionTestCase):
def setUp(self):
self.q = Queue()
self.job = Job.create(func=some_calculation,
args=(3, 4),
kwargs=dict(z=2),
repeat=1,
interval=60)
self.job.save()
def test_enqueue_next(self):
"""Schedule the next job"""
job = self.q.enqueue_next(self.job)
self.assertIsNotNone(job.id)
self.assertNotEqual(job.id, self.job.id)
self.assertEqual(job.scheduled_for,
self.job.scheduled_for + self.job.interval)
def test_schedule_repeat_infinity(self):
"""Schedule repeats for infinity"""
self.job.repeat = -1
job = self.q.enqueue_next(self.job)
self.assertIsNotNone(job.id)
self.assertNotEqual(job.id, self.job.id)
self.assertEqual(job.repeat, self.job.repeat)
def test_schedule_repeat_until(self):
"""Schedule repeat until datetime"""
self.job.repeat = datetime(2999,1,1, tzinfo=utc)
job = self.q.enqueue_next(self.job)
self.assertIsNotNone(job.id)
self.assertNotEqual(job.id, self.job.id)
self.assertEqual(job.repeat, self.job.repeat)
示例2: TestWorkIsUnreadable
class TestWorkIsUnreadable(TransactionTestCase):
def setUp(self):
self.q = Queue()
self.q.save()
self.fq = get_failed_queue()
self.w = Worker.create([self.q])
def test_work_is_unreadable(self):
"""Unreadable jobs are put on the failed queue."""
self.assertEquals(self.fq.count, 0)
self.assertEquals(self.q.count, 0)
# NOTE: We have to fake this enqueueing for this test case.
# What we're simulating here is a call to a function that is not
# importable from the worker process.
job = Job.create(func=div_by_zero, args=(3,))
job.save()
job.instance = 'nonexisting_job'
job.queue = self.q
job.save()
self.assertEquals(self.q.count, 1)
# All set, we're going to process it
self.w.work(burst=True) # should silently pass
self.assertEquals(self.q.count, 0)
self.assertEquals(self.fq.count, 1)
示例3: TestNotify
class TestNotify(TransactionTestCase):
def setUp(self):
self.q = Queue()
def test_notify(self):
"""Postgresql NOTIFY on channel with default connection"""
self.q.notify(1)
示例4: TestWorkerWithJobs
class TestWorkerWithJobs(TransactionTestCase):
def setUp(self):
self.fooq, self.barq = Queue('foo'), Queue('bar')
self.w = Worker.create([self.fooq, self.barq])
self.fooq.enqueue(say_hello, name='Frank')
def test_worker_with_jobs(self):
self.assertEqual(self.w.work(burst=True), True,
'Expected at least some work done.')
示例5: TestPQWorkerSerial
class TestPQWorkerSerial(TransactionTestCase):
reset_sequences = True
def setUp(self):
self.q = Queue()
self.q.save_queue()
self.sq = SerialQueue()
self.sq.save_queue()
def test_pq_worker_serial(self):
call_command('pqworker', 'serial', 'default', burst=True)
示例6: TestDequeueAnySingle
class TestDequeueAnySingle(TransactionTestCase):
def setUp(self):
self.fooq = Queue('foo')
self.barq = Queue('bar')
# Enqueue a single item
self.barq.enqueue(say_hello)
def test_dequeue_any_single(self):
job, queue = PQ.dequeue_any([self.fooq, self.barq], None)
self.assertEqual(job.func, say_hello)
self.assertEqual(queue, self.barq)
示例7: TestRequeueAdminAction
class TestRequeueAdminAction(TestCase):
def setUp(self):
self.q = Queue()
self.q.enqueue_call(div_by_zero, args=(1,))
w = Worker.create(self.q)
w.work(burst=True)
def test_requeue_admin_action(self):
self.assertEqual(0, len(Job.objects.filter(queue_id="default")))
requeue_failed_jobs(None, None, Job.objects.filter(queue_id="failed"))
self.assertEqual(0, len(Job.objects.filter(queue_id="failed")))
self.assertEqual("test_pq.fixtures.div_by_zero", Job.objects.get(queue_id="default").func_name)
示例8: setUp
def setUp(self):
q = Queue()
q.enqueue(say_hello, kwargs={'name':'bob'}, result_ttl=1) # expires
q.enqueue(say_hello, kwargs={'name':'polly'}) # won't expire in this test lifecycle
q.enqueue(say_hello, kwargs={'name':'frank'}, result_ttl=-1) # never expires
w = Worker.create([q])
w.work(burst=True)
q.enqueue(say_hello, kwargs={'name':'david'}) # hasn't run yet
self.q = q
示例9: TestDequeueInstanceMethods
class TestDequeueInstanceMethods(TransactionTestCase):
def setUp(self):
self.q = Queue()
self.c = Calculator(2)
self.result = self.q.enqueue(self.c.calculate, 3, 4)
def test_dequeue_instance_method(self):
"""Dequeueing instance method jobs from queues."""
job = self.q.dequeue()
self.assertEqual(job.func.__name__, 'calculate')
self.assertEqual(job.args, (3, 4))
示例10: TestJobAdmin
class TestJobAdmin(TransactionTestCase):
def setUp(self):
password = 'test'
user = User.objects.create_superuser('test', '[email protected]', password)
self.client.login(username=user.username, password=password)
self.q = Queue()
self.q.enqueue_call(say_hello, args=('you',))
self.q.enqueue_call(div_by_zero, args=(1,))
self.q.schedule(datetime(2099, 1, 1, tzinfo=utc), say_hello, 'later')
w = Worker.create(self.q)
w.work(burst=True)
self.q.enqueue_call(say_hello, args=('me',))
def test_changelist(self):
data = (
("failedjob", FailedJob),
("queuedjob", QueuedJob),
("dequeuedjob", DequeuedJob),
("scheduledjob", ScheduledJob),
)
for modelname, Model in data:
url = reverse("admin:pq_%s_changelist" % modelname)
response = self.client.get(url, follow=True)
msg = "%s != %s -> %s, url: %s" % (
response.status_code, 200, repr(Model), url
)
self.assertEqual(response.status_code, 200, msg)
示例11: TestListenForJobs
class TestListenForJobs(TransactionTestCase):
def setUp(self):
self.q = Queue()
# pre-call this so we don't need to use multi-process
# otherwise this is called within the classmethod
PQ.listen('default', ['default'])
# Fire off a notification of a fake job enqueued
self.q.notify(1)
def test_listen_for_jobs(self):
"""Test the first part of the _listen_for_jobs method which polls
for notifications"""
queue_name = PQ._listen_for_jobs(['default'], 'default', 1)
self.assertEqual('default', queue_name)
示例12: TestEnqueueAsyncFalse
class TestEnqueueAsyncFalse(TestCase):
def setUp(self):
self.q = Queue()
def test_enqueue_async_false(self):
job = self.q.enqueue(some_calculation, args=(2, 3), async=False)
self.assertEqual(job.result, 6)
def test_enqueue_call_async_false(self):
job = self.q.enqueue_call(some_calculation, args=(2, 3), async=False)
self.assertEqual(job.result, 6)
def test_schedule_call_async_false(self):
job = self.q.enqueue_call(some_calculation, args=(2, 3), async=False)
self.assertEqual(job.result, 6)
示例13: TestDequeueOnEmpty
class TestDequeueOnEmpty(TransactionTestCase):
def setUp(self):
self.q = Queue()
def test_pop_job_on_empty(self):
job = self.q.dequeue()
self.assertIsNone(job)
示例14: TestWorkFails
class TestWorkFails(TransactionTestCase):
def setUp(self):
self.q = Queue()
self.fq = get_failed_queue()
self.w = Worker.create([self.q])
self.job = self.q.enqueue(div_by_zero)
self.enqueued_at = self.job.enqueued_at
def test_work_fails(self):
"""Failing jobs are put on the failed queue."""
self.w.work(burst=True) # should silently pass
# Postconditions
self.assertEquals(self.q.count, 0)
self.assertEquals(self.fq.count, 1)
# Check the job
job = Job.objects.get(id=self.job.id)
self.assertEquals(job.origin, self.q.name)
# Should be the original enqueued_at date, not the date of enqueueing
# to the failed queue
self.assertEquals(job.enqueued_at, self.enqueued_at)
self.assertIsNotNone(job.exc_info) # should contain exc_info
示例15: setUp
def setUp(self):
self.q = Queue(scheduled=True)
# simulate the default worker timeout
self.timeout = 60
future = now() + timedelta(seconds=self.timeout/2)
# enqueue a job for 30 seconds time in the future
self.job = self.q.schedule_call(future, do_nothing)