如何从队列中获取值(输入)并计算它们?

时间:2015-11-26 15:58:34

标签: python multithreading python-multiprocessing

使用下面的代码,我将输入值放入队列中。但我似乎无法弄清楚如何将它们排出并计算输入值?

输入应该是这样的:例如1 + 1,5 * 5,10 - 8。然后检查第一个和第二个数字是int,如果它之间有一个运算符,如果正确将它放入队列中。

def producer(queue):
    operators = ('+', '-', '*', '/', '%')

    while True:
        inputs = str(input("Give calculation: ( 1 + 1 , 5 - 2 etc.. type 'end' to end the program ")).split()
        inputs2 = ' '.join(inputs)

        if inputs2 in ['end']:
            print('Bye')
            break

        if inputs[1] not in operators:
            print('Wrong operator input:\n')
            continue

        try:
            int(inputs[0]) and int(inputs[2])
        except ValueError:
            print('Wrong number input: \n')

        else:
            queue.put(inputs2)
            break




def consumer():
    while True:
        q2 = queue.get()
        print (q2)

       #if q2[1] in ['+']:
           #print(int(q2[0]) + int(q2[2]))

queue = multiprocessing.Queue()
t = threading.Thread(target = producer, args=(queue,))
t.start()
t2 = threading.Thread(target = consumer)
t2.start()

0 个答案:

没有答案
相关问题