Skip to main content

A synchronized stack data structure for python 3

Project description

pystack

Synchronized stack data structure for Python 3

Usage

from stack import Stack

stack = Stack()

# push item
stack.push('item')

# pop item
item = stack.pop()

Define size

stack = Stack(10)

Producer Consumer example

from stack import Stack

import random
from threading import Thread
from time import sleep

_stack = Stack(10)

item_number = 0


class Producer(Thread):
    __id = 1

    def __init__(self, stack: Stack):
        super().__init__(name='P-%d' % Producer.__id)
        self.__stack = stack
        Producer.__id = Producer.__id + 1
        pass

    def run(self):
        global item_number
        while (True):
            item = 'item_%d' % item_number
            self.__stack.push(item)
            print(self.name, ' produced ', item)
            item_number = item_number + 1
            sleep(random.randint(0, 1) * 0.3)


class Consumer(Thread):
    __id = 1

    def __init__(self, stack):
        super().__init__(name='C-%d' % Consumer.__id)
        self.__stack = stack
        Consumer.__id = Consumer.__id + 1
        pass

    def run(self):
        global item_number
        while (True):
            item = self.__stack.pop()
            print(self.name, ' consumed ', item)
            item_number = item_number - 1
            sleep(random.randint(0, 1) * 0.3)


p1 = Producer(_stack)
p2 = Producer(_stack)
p3 = Producer(_stack)
c1 = Consumer(_stack)
c2 = Consumer(_stack)
c3 = Consumer(_stack)

p1.start()
p2.start()
p3.start()
c1.start()
c2.start()
c3.start()

LICENSE

Copyright (c) 2018 The Python Packaging Authority

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pystack-sherryt400-0.0.2.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

pystack_sherryt400-0.0.2-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

Details for the file pystack-sherryt400-0.0.2.tar.gz.

File metadata

  • Download URL: pystack-sherryt400-0.0.2.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.8

File hashes

Hashes for pystack-sherryt400-0.0.2.tar.gz
Algorithm Hash digest
SHA256 c2424124691744244a7aace75c8235479ce897dd3bd2db5610c7c961bcc7eca9
MD5 9c25480fd7b65a413b7945b50352754f
BLAKE2b-256 d43a344a96de3553307e3685056116a5e84c365aa86d59b24c96bd70cb1557e8

See more details on using hashes here.

File details

Details for the file pystack_sherryt400-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: pystack_sherryt400-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 3.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.8

File hashes

Hashes for pystack_sherryt400-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 22171a17f9e01abaf5ad61c7ef82dcd672709939bb25a355d5ca794c1ed06a58
MD5 0876424377925b49389e4e374e21a0c4
BLAKE2b-256 ef65b0954526eb530d0d898d92efd67efcf76f570c40222f04282c252a5ed691

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page