0

I am writing a code of the consumer producer problem in python , so far I am almost ready but my code still not running.. Would you help me in any way to finish it, I am new in python and am not sure what I do wrong, any help is appreciated.

Kind regards
Vineet

#!/usr/bin/python
# -*- coding: utf-8 -*-

__author__=



import time 
from  threading import Thread, Lock

wallet = []
lock = Lock()
global obj 

def producer(obj):
    lock.acquire()
    for i in range(10):
        obj = obj + i
        wallet.append(obj)
        time.sleep(1)
    lock.release()


def consumer():
    lock.acquire()
    for i in range(10):
        obj = obj - i
    obj = wallet.pop()
    lock.release()

    return obj    
3
Contributors
4
Replies
63
Views
3 Years
Discussion Span
Last Post by Gribouillis
Featured Replies
  • 1

    You are very far from the solution. Maybe you should try a much easier task first. For example you have no working code. You create a wallet, a lock and a global variable and define two functions. You have a syntax error at line 4 because there is no right … Read More

  • 1

    1. Write any program in python that does something, and does not produce errors when running. Take any beginner book. Get to know with functions, statements, classes, data structures. 2. Google for "python threading tutorial". Read tutorials about threading. For example this link gives almost exactly what you want. http://www.tutorialspoint.com/python/python_multithreading.htm Read More

1

You are very far from the solution.
Maybe you should try a much easier task first.

For example you have no working code. You create a wallet, a lock and a global variable and define two functions. You have a syntax error at line 4 because there is no right hand side value.

Edited by slate: x

0

Thanks for your reply. Can you please tell me what should i do to reach at the solution.

0

I agree with slate. You want a solution, but you didn't even describe the problem that you want to solve. Invoking the 'consumer producer problem' is far too abstract. Didn't you copy and paste code that you don't understand from the internet ?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.