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    

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.

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

  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
commented: wise advice +14

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 ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.