Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A synchronized persistent Queue class

License

NotificationsYou must be signed in to change notification settings

balena/python-pqueue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pqueue is a simple persistent (disk-based) FIFO queue for Python.

pqueue goals are speed and simplicity. The development was initially basedon theQueuelib code. Entries are saved on disk usingpickle.

Requirements

  • Python 2.7 or Python 3.x
  • no external libraries requirements

Installation

You can installpqueue either via Python Package Index (PyPI) or fromsource.

To install using pip:

$ pip install pqueue

To install using easy_install:

$ easy_install pqueue

If you have downloaded a source tarball you can install it by running thefollowing (as root):

# python setup.py install

How to use

pqueue provides a single FIFO queue implementation.

Here is an example usage of the FIFO queue:

>>> from pqueue import Queue>>> q = Queue("tmpqueue")>>> q.put(b'a')>>> q.put(b'b')>>> q.put(b'c')>>> q.get()b'a'>>> del q>>> q = Queue("tmpqueue")>>> q.get()b'b'>>> q.get()b'c'>>> q.get_nowait()Traceback (most recent call last):  File "<stdin>", line 1, in <module>  File "/usr/lib/python2.7/Queue.py", line 190, in get_nowait    return self.get(False)  File "/usr/lib/python2.7/Queue.py", line 165, in get    raise EmptyQueue.Empty

TheQueue object is identical to Python'sQueue module (orqueue inPython 3.x), with the difference that it requires a parameterpathindicating where to persist the queue data andchunksize indicating howmany enqueued items should be stored per file. The samemaxsize parameteravailable on the system wiseQueue has been maintained.

In other words, it works exactly as Python'sQueue, with the difference anyabrupt interruption isACID-guaranteed:

q = Queue()def worker():    while True:        item = q.get()        do_work(item)        q.task_done()for i in range(num_worker_threads):     t = Thread(target=worker)     t.daemon = True     t.start()for item in source():    q.put(item)q.join()       # block until all tasks are done

Note that pqueueis not intended to be used by multiple processes.

How it works?

Pushed data is serialized using pickle in sequence, on chunked files named asqNNNNN, with a maximum ofchunksize elements, all stored on the givenpath.

The queue is formed by ahead and atail. Pushed data goes onhead,pulled data goes ontail.

Aninfo file is pickled in thepath, having the followingdict:

  • head: a list of three integers, an index of thehead file, the numberof elements written, and the file position of the last write.
  • tail: a list of three integers, an index of thetail file, the numberof elements read, and the file position of the last read.
  • size: number of elements in the queue.
  • chunksize: number of elements that should be stored in each disk queuefile.

Both read and write operations depend on sequential transactions on disk. Inorder to accomplish ACID requirements, these modifications are protected by theQueue locks.

If, for any reason, the application stops working in the middle of a headwrite, a second execution will remove any inconsistency by truncating thepartial head write.

Onget, theinfo file is not updated, only when you first calltask_done, and only on the first time case you have to call itsequentially.

Theinfo file is updated in the following way: a temporary file (using'mkstemp') is created with the new data and then moved over the previousinfo file. This was designed this way as POSIX 'rename' is guaranteed to beatomic.

In case of abrupt interruptions, one of the following conditions may happen:

  • A partial write of the last pushed element may occur and in this case onlythis last element pushed will be discarded.
  • An element pulled from the queue may be processing, and in this case a secondrun will consume same element again.

Tests

Tests are located inpqueue/tests directory. They can be run usingPython's defaultunittest module with the following command:

./runtests.py

The output should be something like the following:

./runtests.pytest_GarbageOnHead (pqueue.tests.test_queue.PersistenceTest)Adds garbage to the queue head and let the internal integrity ... oktest_MultiThreaded (pqueue.tests.test_queue.PersistenceTest)Create consumer and producer threads, check parallelism ... oktest_OpenCloseOneHundred (pqueue.tests.test_queue.PersistenceTest)Write 1000 items, close, reopen checking if all items are there ... oktest_OpenCloseSingle (pqueue.tests.test_queue.PersistenceTest)Write 1 item, close, reopen checking if same item is there ... oktest_PartialWrite (pqueue.tests.test_queue.PersistenceTest)Test recovery from previous crash w/ partial write ... oktest_RandomReadWrite (pqueue.tests.test_queue.PersistenceTest)Test random read/write ... ok----------------------------------------------------------------------Ran 6 tests in 1.301sOK

License

This software is licensed under the BSD License. See the LICENSE file in thetop distribution directory for the full license text.

Versioning

This software followsSemantic Versioning

About

A synchronized persistent Queue class

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp