2016-03-09 2 views
-1

im having problem Изменение сценария, так что структура данных, совместно используемая между производителем и потребителем, представляет собой список из 2 -tuples вместо списка строк , и я не могу понять, что происходит не так.python Traceback (последний последний вызов): & TypeError: может только конкатенация кортежа (не «str») на кортеж

#!/usr/bin/env python 
    # demonstrate coroutines in Python 
    """ 
    + implements a typical producer/consumer algorithm 
    + the consumer is a subroutine and main() is the producer 
    + producer sends a job request to consumer; waits for consumer to receive it 
    + the consumer waits for job request, does job, waits again 
    + keywords: 
    + yield waits on producer - passes argument to producer at handoff 
    + next() sends job to consumer w/o msg 
    + send() sends job to consumer with msg 
    """ 
    import sys 
    def printjob(name): 
    name += " " 
    sys.stdout.write(name) 
    """ 
+ CONSUMER 
+ 'yield stuff' passes stuff back to the producer; when control resumes a 
+ message (it may be empty) is available from the producer as the return 
+ value from yield; note: cannot remove everything from the list since 
+ the dereference to jobs[i] in yield is invalid 
""" 
def consumer(jobs): 
    i = -1 
# as long as something is in the jobs list keep processing requests 
    while jobs: 
     i = (i + 1) % len(jobs) 
     # yield passes control back to producer with the ith job name 
     getRequest = yield jobs[i] # waits for request from producer 
     if getRequest: # if getRequest is not empty process it 
      request,name,num = getRequest 
      if request == "add": 
       jobs.append(name) 
       jobs.append(num) 
       sys.stdout.write("\nADD ") 
      elif request == "remove" and name in jobs: 
       jobs.remove(name) 
       buf = "\nREMOVE " + name + "\n" 
       sys.stdout.write(buf) 
    print "\nNo jobs left to do!\n" 
def producer(jobs): 
    con = consumer(jobs)     # start the consumer 
    buf = "Initial job list (" + str(len(jobs)) + "): " 
    sys.stdout.write(buf) 
    for i in range(len(jobs)): 
     printjob(con.next())   # next sends job to consumer w/ no msg 
    printjob(con.send(("add", "iron",44))) # send sends job to consumer w/ msg 
    sys.stdout.write("\n") 
    for i in range(len(jobs)): 
     printjob(con.next()) 
    con.send(("remove","fold",33)) 
    for i in range(len(jobs)): 
     printjob(con.next()) 
    con.send(("remove","wash",11)) 
    for i in range(len(jobs)): 
     printjob(con.next()) 
    print "\nProducer Done." 
""" 
+ MAIN 
+ acts as the producer coroutine 
+ next passes a job to the consumer with no message passing 
+ send passes a job to the consumer with a message 
""" 
if __name__ == "__main__":    # this means initialize once only 
    jobs = [("wash",11),("dry",22),("fold",33)]  # tuble list 
    pro = producer(jobs) 

Любая помощь была бы принята с благодарностью!

+0

Пожалуйста, разместите полную информацию о трассе. – kindall

ответ

0

ошибка, я подозреваю, указывая на этой линии:

name += " " 

... потому что вы (именно так, как говорит ошибка) попытка объединить строку " " в кортеж ("wash",11). Что именно вы пытаетесь сделать с этой линией? Если вы пытаетесь просто напечатать некоторую строчную форму кортежа, используйте функцию str:

sys.stdout.write(str(name) + '\n') 
Смежные вопросы