当前位置: 首页>>代码示例>>Python>>正文


Python Queue.enqueue方法代码示例

本文整理汇总了Python中pq.Queue.enqueue方法的典型用法代码示例。如果您正苦于以下问题:Python Queue.enqueue方法的具体用法?Python Queue.enqueue怎么用?Python Queue.enqueue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pq.Queue的用法示例。


在下文中一共展示了Queue.enqueue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestWorkerWithJobs

# 需要导入模块: from pq import Queue [as 别名]
# 或者: from pq.Queue import enqueue [as 别名]
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.')
开发者ID:oinopion,项目名称:django-pq,代码行数:11,代码来源:test_worker.py

示例2: TestDequeueAnySingle

# 需要导入模块: from pq import Queue [as 别名]
# 或者: from pq.Queue import enqueue [as 别名]
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)
开发者ID:oinopion,项目名称:django-pq,代码行数:13,代码来源:test_queue.py

示例3: TestWorkFails

# 需要导入模块: from pq import Queue [as 别名]
# 或者: from pq.Queue import enqueue [as 别名]
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
开发者ID:vilos,项目名称:django-pq,代码行数:28,代码来源:test_worker.py

示例4: TestWorkerTimeouts

# 需要导入模块: from pq import Queue [as 别名]
# 或者: from pq.Queue import enqueue [as 别名]
class TestWorkerTimeouts(TransactionTestCase):

    def setUp(self):
        self.sentinel_file = '/tmp/.rq_sentinel'
        self.q = Queue()
        self.fq = get_failed_queue()
        self.w = Worker.create([self.q])

    def test_timeouts(self):
        """Worker kills jobs after timeout."""

        # Put it on the queue with a timeout value
        jobr = self.q.enqueue(
                create_file_after_timeout,
                args=(self.sentinel_file, 4),
                timeout=1)

        self.assertEquals(os.path.exists(self.sentinel_file), False)
        self.w.work(burst=True)
        self.assertEquals(os.path.exists(self.sentinel_file), False)

        job = Job.objects.get(id=jobr.id)
        self.assertIn('JobTimeoutException', job.exc_info)

    def tearDown(self):
        try:
            os.unlink(self.sentinel_file)
        except OSError as e:
            if e.errno == 2:
                pass
开发者ID:vilos,项目名称:django-pq,代码行数:32,代码来源:test_worker.py

示例5: TestWorkerCustomExcHandling

# 需要导入模块: from pq import Queue [as 别名]
# 或者: from pq.Queue import enqueue [as 别名]
class TestWorkerCustomExcHandling(TransactionTestCase):

    def setUp(self):
        self.q = Queue()
        self.fq = get_failed_queue()
        def black_hole(job, *exc_info):
            # Don't fall through to default behaviour of moving to failed queue
            return False
        self.black_hole = black_hole
        self.job = self.q.enqueue(div_by_zero)



    def test_custom_exc_handling(self):
        """Custom exception handling."""


        w = Worker.create([self.q], exc_handler=self.black_hole)
        w.work(burst=True)  # should silently pass

        # Postconditions
        self.assertEquals(self.q.count, 0)
        self.assertEquals(self.fq.count, 0)

        # Check the job
        job = Job.objects.get(id=self.job.id)
        self.assertEquals(job.status, Job.FAILED)
开发者ID:vilos,项目名称:django-pq,代码行数:29,代码来源:test_worker.py

示例6: setUp

# 需要导入模块: from pq import Queue [as 别名]
# 或者: from pq.Queue import enqueue [as 别名]
 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
开发者ID:vladignatyev,项目名称:django-pq,代码行数:11,代码来源:test_queue.py

示例7: TestQueueInstanceMethods

# 需要导入模块: from pq import Queue [as 别名]
# 或者: from pq.Queue import enqueue [as 别名]
class TestQueueInstanceMethods(TransactionTestCase):
    def setUp(self):
        self.q = Queue()

    def test_enqueue(self):  # noqa
        """Enqueueing job onto queues."""

        # say_hello spec holds which queue this is sent to
        job = self.q.enqueue(say_hello, 'Nick', foo='bar')
        self.assertEqual(job.queue, self.q)
开发者ID:oinopion,项目名称:django-pq,代码行数:12,代码来源:test_queue.py

示例8: TestWorkViaStringArg

# 需要导入模块: from pq import Queue [as 别名]
# 或者: from pq.Queue import enqueue [as 别名]
class TestWorkViaStringArg(TransactionTestCase):
    def setUp(self):
        self.q = Queue('foo')
        self.w = Worker.create([self.q])
        self.job = self.q.enqueue('test_pq.fixtures.say_hello', name='Frank')

    def test_work_via_string_argument(self):
        """Worker processes work fed via string arguments."""

        self.assertEquals(self.w.work(burst=True), True,
                'Expected at least some work done.')
        job = Job.objects.get(id=self.job.id)
        self.assertEquals(job.result, 'Hi there, Frank!')
开发者ID:vilos,项目名称:django-pq,代码行数:15,代码来源:test_worker.py

示例9: TestWorkerDeletesExpiredTTL

# 需要导入模块: from pq import Queue [as 别名]
# 或者: from pq.Queue import enqueue [as 别名]
class TestWorkerDeletesExpiredTTL(TransactionTestCase):
    def setUp(self):
        self.q = Queue()
        self.w = Worker.create([self.q])
        self.job = self.q.enqueue(say_hello, args=('Bob',), result_ttl=1)
        self.w.work(burst=True)

    def test_worker_deletes_expired_ttl(self):
        """Ensure that Worker deletes expired jobs"""
        time.sleep(1)
        self.w.work(burst=True)
        with self.assertRaises(Job.DoesNotExist):
            Job.objects.get(id=self.job.id)
开发者ID:oinopion,项目名称:django-pq,代码行数:15,代码来源:test_worker.py

示例10: TestDequeueInstanceMethods

# 需要导入模块: from pq import Queue [as 别名]
# 或者: from pq.Queue import enqueue [as 别名]
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))
开发者ID:oinopion,项目名称:django-pq,代码行数:15,代码来源:test_queue.py

示例11: TestDequeueAnyMultiple

# 需要导入模块: from pq import Queue [as 别名]
# 或者: from pq.Queue import enqueue [as 别名]
class TestDequeueAnyMultiple(TransactionTestCase):
    def setUp(self):
        self.fooq = Queue('foo')
        self.barq = Queue('bar')
        # Enqueue items on both queues
        self.barq.enqueue(say_hello, 'for Bar')
        self.fooq.enqueue(say_hello, 'for Foo')

    def test_dequeue_any_multiple(self):
        job, queue = PQ.dequeue_any([self.fooq, self.barq], None)
        self.assertEqual(queue, self.fooq)
        self.assertEqual(job.func, say_hello)
        self.assertEqual(job.origin, self.fooq.name)
        self.assertEqual(
            job.args[0], 'for Foo', 'Foo should be dequeued first.')

        job, queue = PQ.dequeue_any([self.fooq, self.barq], None)
        self.assertEqual(queue, self.barq)
        self.assertEqual(job.func, say_hello)
        self.assertEqual(job.origin, self.barq.name)
        self.assertEqual(
            job.args[0], 'for Bar', 'Bar should be dequeued second.')
开发者ID:oinopion,项目名称:django-pq,代码行数:24,代码来源:test_queue.py

示例12: TestEnqueueAsyncFalse

# 需要导入模块: from pq import Queue [as 别名]
# 或者: from pq.Queue import enqueue [as 别名]
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)
开发者ID:oinopion,项目名称:django-pq,代码行数:17,代码来源:test_queue.py

示例13: TestWorkerSetsResultTTL

# 需要导入模块: from pq import Queue [as 别名]
# 或者: from pq.Queue import enqueue [as 别名]
class TestWorkerSetsResultTTL(TransactionTestCase):

    def setUp(self):
        self.q = Queue()
        self.w = Worker.create([self.q])

    @params((10,10), (-1,-1), (0, None))
    def test_worker_sets_result_ttl(self, ttl, outcome):
        """Ensure that Worker properly sets result_ttl for individual jobs or deletes them."""
        job = self.q.enqueue(say_hello, args=('Frank',), result_ttl=ttl)
        self.w.work(burst=True)
        try:
            rjob = Job.objects.get(id=job.id)
            result_ttl = rjob.result_ttl
        except Job.DoesNotExist:
            result_ttl = None

        self.assertEqual(result_ttl, outcome)
开发者ID:vilos,项目名称:django-pq,代码行数:20,代码来源:test_worker.py

示例14: Test_get_job_or_promise

# 需要导入模块: from pq import Queue [as 别名]
# 或者: from pq.Queue import enqueue [as 别名]
class Test_get_job_or_promise(TransactionTestCase):
    """Test the Job._get_job_or_promise classmethod"""

    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)


    def test_get_job_or_promise(self):
        """Test get a promise of a job in the future"""

        job, promise, timeout = Job._get_job_or_promise(
            self.q.connection, self.q, self.timeout)
        self.assertLessEqual(timeout, self.timeout)
        self.assertIsNone(job)
        self.assertEqual(promise, self.q.name)

    def test_get_no_job_no_promise(self):
        """Test get no job and no promise"""

        # job is in the future beyond the current
        # worker timeout
        job, promise, timeout = Job._get_job_or_promise(
            self.q.connection, self.q, 1)
        self.assertEqual(timeout, 1)
        self.assertIsNone(job)
        self.assertIsNone(promise)

    def test_get_earlier_job_no_promise(self):
        """Test get earlier job and no promise"""
        # Job enqueue after the first scheduled job
        # but to be exec ahead of the scheduled job
        now_job = self.q.enqueue(do_nothing)
        job, promise, timeout = Job._get_job_or_promise(
            self.q.connection, self.q, 60)
        # timeout should remain the same
        self.assertEqual(timeout, 60)
        self.assertEqual(now_job.id, job.id)
        self.assertIsNone(promise)
开发者ID:JirkaV,项目名称:django-pq,代码行数:45,代码来源:test_job.py

示例15: TestDequeue

# 需要导入模块: from pq import Queue [as 别名]
# 或者: from pq.Queue import enqueue [as 别名]
class TestDequeue(TransactionTestCase):
    def setUp(self):
        self.q = Queue()
        self.result = self.q.enqueue(say_hello, 'Rick', foo='bar')
        # self.result2 = q.enqueue(c.calculate, 3, 4)
        # self.c = Calculator(2)

    def test_dequeue(self):
        """Dequeueing jobs from queues."""

        # Dequeue a job (not a job ID) off the queue
        self.assertEqual(self.q.count, 1)
        job = self.q.dequeue()
        self.assertEqual(job.id, self.result.id)
        self.assertEqual(job.func, say_hello)
        self.assertEqual(job.origin, self.q.name)
        self.assertEqual(job.args[0], 'Rick')
        self.assertEqual(job.kwargs['foo'], 'bar')

        # ...and assert the queue count when down
        self.assertEqual(self.q.count, 0)
开发者ID:oinopion,项目名称:django-pq,代码行数:23,代码来源:test_queue.py


注:本文中的pq.Queue.enqueue方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。