Discuss / Python / 子进程中raw_input报错

子进程中raw_input报错

Colben_L

#1 Created at ... [Delete] [Delete and Lock User]

环境:Ubuntu14.04 python2.7.6

源码:

#=========================================
# Filename : a.py
# Filetype : Python
# Author   : Colben
# Tele     : 13552112721
# Company  : BoyaSoftware
# Create   : 2014-11-20 14:01:56
#=========================================

#!/usr/bin/python

from multiprocessing import Process, Queue
import os, time, random

def q_write(queue):
    enter = ''
    while 'quit' != enter:
        enter = raw_input('Colben_cmd>>')
        queue.put(enter, block = True)
    print 'End of q_write'

def q_read(queue):
    value = ''
    while 'quit' != value:
        value = queue.get(block = True)
        print 'Return>>%s.'%(value)
    print 'End of q_read'

if '__main__' == __name__:
    q = Queue()
    sub_write = Process(target = q_write, args = (q, ))
    sub_read = Process(target = q_read, args = (q, ))
    sub_write.start()
    sub_read.start()
    sub_write.join()
    sub_read.join()
    print 'End of parent process'

执行 python a.py 出错信息:

``` Process Process-1: Traceback (most recent call last): File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap self.run() File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run self._target(self._args, *self._kwargs) File "a.py", line 18, in q_write enter = raw_input('Colben_cmd>>') EOFError: EOF when reading a line Colben_cmd>>

Colben_L

#2 Created at ... [Delete] [Delete and Lock User]

输入abcdefg 回车后终端无反应,按下Ctrl+C后,终端又显示如下信息

Colben_cmd>>abcdefg

^CTraceback (most recent call last):
  File "a.py", line 36, in <module>
    sub_read.join()
  File "/usr/lib/python2.7/multiprocessing/process.py", line 145, in join
Process Process-2:
    res = self._popen.wait(timeout)
  File "/usr/lib/python2.7/multiprocessing/forking.py", line 154, in wait
Traceback (most recent call last):
    return self.poll(0)
  File "/usr/lib/python2.7/multiprocessing/forking.py", line 135, in poll
  File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    pid, sts = os.waitpid(self.pid, flag)
KeyboardInterrupt
    self.run()
  File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "a.py", line 25, in q_read
    value = queue.get(block = True)
  File "/usr/lib/python2.7/multiprocessing/queues.py", line 117, in get
    res = self._recv()
KeyboardInterrupt

求大神指点!!!!!!


  • 1

Reply