Coder Social home page Coder Social logo

python-tutorials's People

Contributors

laurentluce avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

python-tutorials's Issues

One question about condition

I want to ask a question. In the producer-consumer with condition program, why there must be a two-layer while in the run() of consumer? I tried to write a consumer run() only with one while like this:
while True: self.condition.acquire( if len(self.integers) == 0: self.condition.wait() integer = self.integers.pop() print integer self.condition.release()
The program will crash for pop an empty list. For count of notify is same as the count of total integers appended to the list, before pop there must be at least one integer in the list, why wait() will return without a notify?

Example with events is flawed

The blog post at http://www.laurentluce.com/posts/python-threads-synchronization-locks-rlocks-semaphores-conditions-events-and-queues/ is often quoted, for example from stack overflow answers.
However, the producer/consumer example using events is flawed and might make people implement this common pattern in a wrong way. The problem is as follows:

It's possible that the producer calls event.set() and event.clear() while the consumer is still processing the last integer and thus is not currently waiting for the event. By the time the consumer calls event.wait() again, the producer's call to event.clear() may have already completed and the consumer will then wait when it shouldn't, given that there's now an integer ready to be processed. Running the example code as-is multiple times shows this problem: the "xx popped from list" line is sometimes not printed 10 times as it should.

Given that this blog post has quite some exposure, it would make sense to fix this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.