python 2.7多线程挂起打印

时间:2017-11-10 21:10:17

标签: python multithreading python-2.7 deadlock hang

我的python软件间歇性地死锁(可能是由于一些竞争条件)

在其中一个正在运行的线程中,它运行

while True:
   msg = printQ.get()
   print msg
   sys.stdout.flush()

在调试时,我看到我的一个帖子被阻止了 sys.stdout.flush()。如果我删除了冲洗线,则线程在打印时挂起。 这是打印到stdout的唯一线程

如果我禁用此线程,我的软件不会死锁

在多线程环境中刷新stdout是否有任何已知问题?

这些是软件死锁时运行线程的跟踪。有什么东西跳到你们身边吗?对我来说,它看起来像几个睡眠,等待队列中的项目。对我来说唯一奇怪的是sys.stdout.flush()

# ThreadID: 139736001795840
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 783, in __bootstrap
  self.__bootstrap_inner()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 810, in __bootstrap_inner
  self.run()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 763, in run
  self.__target(*self.__args, **self.__kwargs)
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Dispatch_Factory.py", line 726, in worker
  jobref = Host.r.blpop(Dispatch.rkey_completeQ, timeout=1)
File: "/usr/software/rats/bedrock/lib/python2.7/site-packages/redis/client.py", line 1090, in blpop
  return self.execute_command('BLPOP', *keys)
File: "/usr/software/rats/bedrock/lib/python2.7/site-packages/redis/client.py", line 529, in execute_command
  return self.parse_response(connection, command_name, **options)
File: "/usr/software/rats/bedrock/lib/python2.7/site-packages/redis/client.py", line 541, in parse_response
  response = connection.read_response()
File: "/usr/software/rats/bedrock/lib/python2.7/site-packages/redis/connection.py", line 545, in read_response
  response = self._parser.read_response()
File: "/usr/software/rats/bedrock/lib/python2.7/site-packages/redis/connection.py", line 213, in read_response
  response = self._buffer.readline()
File: "/usr/software/rats/bedrock/lib/python2.7/site-packages/redis/connection.py", line 151, in readline
  self._read_from_socket()
File: "/usr/software/rats/bedrock/lib/python2.7/site-packages/redis/connection.py", line 109, in _read_from_socket
  data = self._sock.recv(socket_read_size)

# ThreadID: 139736012285696
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 783, in __bootstrap
  self.__bootstrap_inner()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 810, in __bootstrap_inner
  self.run()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 763, in run
  self.__target(*self.__args, **self.__kwargs)
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Dispatch_Factory.py", line 605, in worker
  job = Factory.dispQ.get()
File: "/usr/software/rats/bedrock/lib/python2.7/Queue.py", line 168, in get
  self.not_empty.wait()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 340, in wait
  waiter.acquire()

# ThreadID: 139736022775552
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 783, in __bootstrap
  self.__bootstrap_inner()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 810, in __bootstrap_inner
  self.run()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 763, in run
  self.__target(*self.__args, **self.__kwargs)
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Dispatch_Factory.py", line 605, in worker
  job = Factory.dispQ.get()
File: "/usr/software/rats/bedrock/lib/python2.7/Queue.py", line 168, in get
  self.not_empty.wait()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 340, in wait
  waiter.acquire()

# ThreadID: 139737333483264
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 783, in __bootstrap
  self.__bootstrap_inner()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 810, in __bootstrap_inner
  self.run()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 763, in run
  self.__target(*self.__args, **self.__kwargs)
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Dispatch_Factory.py", line 236, in worker
  (job, msg, to_debug) = Factory.logQ.get()
File: "/usr/software/rats/bedrock/lib/python2.7/Queue.py", line 168, in get
  self.not_empty.wait()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 340, in wait
  waiter.acquire()

# ThreadID: 139736461068032
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 783, in __bootstrap
  self.__bootstrap_inner()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 810, in __bootstrap_inner
  self.run()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 763, in run
  self.__target(*self.__args, **self.__kwargs)
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Dispatch_Factory.py", line 605, in worker
  job = Factory.dispQ.get()
File: "/usr/software/rats/bedrock/lib/python2.7/Queue.py", line 168, in get
  self.not_empty.wait()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 340, in wait
  waiter.acquire()

# ThreadID: 139736492537600
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 783, in __bootstrap
  self.__bootstrap_inner()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 810, in __bootstrap_inner
  self.run()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 763, in run
  self.__target(*self.__args, **self.__kwargs)
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Dispatch_Factory.py", line 470, in worker
  job = Factory.rdyQ.get()
File: "/usr/software/rats/bedrock/lib/python2.7/Queue.py", line 168, in get
  self.not_empty.wait()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 340, in wait
  waiter.acquire()

# ThreadID: 139737322993408
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 783, in __bootstrap
  self.__bootstrap_inner()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 810, in __bootstrap_inner
  self.run()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 763, in run
  self.__target(*self.__args, **self.__kwargs)
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Dispatch_Factory.py", line 313, in worker
  sys.stdout.flush()

# ThreadID: 139736909850368
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 783, in __bootstrap
  self.__bootstrap_inner()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 810, in __bootstrap_inner
  self.run()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 763, in run
  self.__target(*self.__args, **self.__kwargs)
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Dispatch_Factory.py", line 470, in worker
  job = Factory.rdyQ.get()
File: "/usr/software/rats/bedrock/lib/python2.7/Queue.py", line 168, in get
  self.not_empty.wait()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 340, in wait
  waiter.acquire()

# ThreadID: 139736920340224
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 783, in __bootstrap
  self.__bootstrap_inner()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 810, in __bootstrap_inner
  self.run()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 763, in run
  self.__target(*self.__args, **self.__kwargs)
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Dispatch_Factory.py", line 470, in worker
  job = Factory.rdyQ.get()
File: "/usr/software/rats/bedrock/lib/python2.7/Queue.py", line 168, in get
  self.not_empty.wait()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 340, in wait
  waiter.acquire()

# ThreadID: 139737354462976
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 783, in __bootstrap
  self.__bootstrap_inner()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 810, in __bootstrap_inner
  self.run()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 763, in run
  self.__target(*self.__args, **self.__kwargs)
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Dispatch_Factory.py", line 350, in worker
  item = Factory.streamQ.get()
File: "/usr/software/rats/bedrock/lib/python2.7/Queue.py", line 168, in get
  self.not_empty.wait()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 340, in wait
  waiter.acquire()

# ThreadID: 139736930830080
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 783, in __bootstrap
  self.__bootstrap_inner()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 810, in __bootstrap_inner
  self.run()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 763, in run
  self.__target(*self.__args, **self.__kwargs)
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Dispatch_Factory.py", line 470, in worker
  job = Factory.rdyQ.get()
File: "/usr/software/rats/bedrock/lib/python2.7/Queue.py", line 168, in get
  self.not_empty.wait()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 340, in wait
  waiter.acquire()

# ThreadID: 139736471557888
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 783, in __bootstrap
  self.__bootstrap_inner()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 810, in __bootstrap_inner
  self.run()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 763, in run
  self.__target(*self.__args, **self.__kwargs)
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Dispatch_Factory.py", line 470, in worker
  job = Factory.rdyQ.get()
File: "/usr/software/rats/bedrock/lib/python2.7/Queue.py", line 168, in get
  self.not_empty.wait()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 340, in wait
  waiter.acquire()

# ThreadID: 139736951809792
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 783, in __bootstrap
  self.__bootstrap_inner()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 810, in __bootstrap_inner
  self.run()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 763, in run
  self.__target(*self.__args, **self.__kwargs)
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Dispatch_Factory.py", line 470, in worker
  job = Factory.rdyQ.get()
File: "/usr/software/rats/bedrock/lib/python2.7/Queue.py", line 168, in get
  self.not_empty.wait()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 340, in wait
  waiter.acquire()

# ThreadID: 139736962299648
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 783, in __bootstrap
  self.__bootstrap_inner()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 810, in __bootstrap_inner
  self.run()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 763, in run
  self.__target(*self.__args, **self.__kwargs)
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Dispatch_Factory.py", line 470, in worker
  job = Factory.rdyQ.get()
File: "/usr/software/rats/bedrock/lib/python2.7/Queue.py", line 168, in get
  self.not_empty.wait()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 340, in wait
  waiter.acquire()

# ThreadID: 139737474270976
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 783, in __bootstrap
  self.__bootstrap_inner()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 810, in __bootstrap_inner
  self.run()
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Status_thread.py", line 131, in run
  request = self._queue.get(timeout=1) # blocking get with a timeout of 1 second
File: "/usr/software/rats/bedrock/lib/python2.7/Queue.py", line 177, in get
  self.not_empty.wait(remaining)
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 359, in wait
  _sleep(delay)

# ThreadID: 139736450578176
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 783, in __bootstrap
  self.__bootstrap_inner()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 810, in __bootstrap_inner
  self.run()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 763, in run
  self.__target(*self.__args, **self.__kwargs)
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Dispatch_Factory.py", line 605, in worker
  job = Factory.dispQ.get()
File: "/usr/software/rats/bedrock/lib/python2.7/Queue.py", line 168, in get
  self.not_empty.wait()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 340, in wait
  waiter.acquire()

# ThreadID: 139737364952832
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 783, in __bootstrap
  self.__bootstrap_inner()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 810, in __bootstrap_inner
  self.run()
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Stack_Tracer.py", line 56, in run
  self.stacktraces()
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Stack_Tracer.py", line 70, in stacktraces
  fout.write(stacktraces())
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Stack_Tracer.py", line 22, in stacktraces
  for filename, lineno, name, line in traceback.extract_stack(stack):

# ThreadID: 139737343973120
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 783, in __bootstrap
  self.__bootstrap_inner()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 810, in __bootstrap_inner
  self.run()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 763, in run
  self.__target(*self.__args, **self.__kwargs)
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Dispatch_Factory.py", line 416, in worker
  time.sleep(1)

# ThreadID: 139736440088320
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 783, in __bootstrap
  self.__bootstrap_inner()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 810, in __bootstrap_inner
  self.run()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 763, in run
  self.__target(*self.__args, **self.__kwargs)
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Dispatch_Factory.py", line 605, in worker
  job = Factory.dispQ.get()
File: "/usr/software/rats/bedrock/lib/python2.7/Queue.py", line 168, in get
  self.not_empty.wait()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 340, in wait
  waiter.acquire()

# ThreadID: 139736482047744
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 783, in __bootstrap
  self.__bootstrap_inner()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 810, in __bootstrap_inner
  self.run()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 763, in run
  self.__target(*self.__args, **self.__kwargs)
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Dispatch_Factory.py", line 470, in worker
  job = Factory.rdyQ.get()
File: "/usr/software/rats/bedrock/lib/python2.7/Queue.py", line 168, in get
  self.not_empty.wait()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 340, in wait
  waiter.acquire()

# ThreadID: 139737312503552
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 783, in __bootstrap
  self.__bootstrap_inner()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 810, in __bootstrap_inner
  self.run()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 763, in run
  self.__target(*self.__args, **self.__kwargs)
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Dispatch_Factory.py", line 470, in worker
  job = Factory.rdyQ.get()
File: "/usr/software/rats/bedrock/lib/python2.7/Queue.py", line 168, in get
  self.not_empty.wait()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 340, in wait
  waiter.acquire()

# ThreadID: 139737628997376
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Br.py", line 86, in <module>
  Command.main()
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Command.py", line 149, in main
  com.run(args)
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Btool.py", line 181, in run
  self.make_locked(args)
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Btool.py", line 227, in make_locked
  Dispatch.run_jobs()
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Dispatch.py", line 732, in run_jobs
  # we got any hung jobs??
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Dispatch.py", line 332, in check_for_hung_jobs
  really_running = job.log_check()
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Job.py", line 391, in log_check
  if Checkproc.check_remote_process(Options.options.rsh_cmd, lock.hostname, lock.pid, 20):
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Checkproc.py", line 92, in check_remote_process
  time.sleep(1)

# ThreadID: 139736941319936
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 783, in __bootstrap
  self.__bootstrap_inner()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 810, in __bootstrap_inner
  self.run()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 763, in run
  self.__target(*self.__args, **self.__kwargs)
File: "/u/eugenep/bedrock/source/br-new-release/lib/btool/Dispatch_Factory.py", line 470, in worker
  job = Factory.rdyQ.get()
File: "/usr/software/rats/bedrock/lib/python2.7/Queue.py", line 168, in get
  self.not_empty.wait()
File: "/usr/software/rats/bedrock/lib/python2.7/threading.py", line 340, in wait
  waiter.acquire()

1 个答案:

答案 0 :(得分:0)

AFAIK sys.stdout实际上不是线程安全的,但没有任何代码,实际上不可能说。出于调试目的,您可以尝试锁定sys.stdout.flush,例如:

orig_flush = sys.stdout.flush
flush_lock = threading.Lock()

def myflush():  
    with flush_lock:
        orig_flush()

sys.stdout.flush = myflush

# ... eventually:
sys.stdout.flush = orig_flush