99热99这里只有精品6国产,亚洲中文字幕在线天天更新,在线观看亚洲精品国产福利片 ,久久久久综合网

歡迎加入QQ討論群258996829
麥子學(xué)院 頭像
蘋果6袋
6
麥子學(xué)院

Python進(jìn)程間通信之命名管道(Windows)

發(fā)布時(shí)間:2017-09-26 14:12  回復(fù):0  查看:3150   最后回復(fù):2017-09-26 14:12  

本文和大家分享的主要是windows系統(tǒng)下,python進(jìn)程間通信的命名管道相關(guān)內(nèi)容 ,一起來(lái)看看吧,希望對(duì)大家學(xué)習(xí)python有所幫助。

  在 Windows 上的命名管道主要是通過(guò)調(diào)用 win32 api 的以下方法來(lái)實(shí)現(xiàn)的:

  - win32pipe.CreateNamedPipe()

  - win32pipe.ConnectNamedPipe()

  - win32file.ReadFile()

  - win32file.WriteFile()

  下面看一個(gè)例子,比較簡(jiǎn)單,只是需要注意一下命名管道的命名規(guī)則。

  server.py

  import win32fileimport win32pipe

  PIPE_NAME = r'\\\\.\\pipe\\test_pipe'

  PIPE_BUFFER_SIZE = 65535

  while True:

  named_pipe = win32pipe.CreateNamedPipe(PIPE_NAME,

  win32pipe.PIPE_ACCESS_DUPLEX,

  win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT | win32pipe.PIPE_READMODE_MESSAGE,

  win32pipe.PIPE_UNLIMITED_INSTANCES,

  PIPE_BUFFER_SIZE,

  PIPE_BUFFER_SIZE, 500, None)

  try:

  while True:

  try:

  win32pipe.ConnectNamedPipe(named_pipe, None)

  data = win32file.ReadFile(named_pipe, PIPE_BUFFER_SIZE, None)

  if data is None or len(data) < 2:

  continue

  print 'receive msg:', data

  except BaseException as e:

  print "exception:", e

  break

  finally:

  try:

  win32pipe.DisconnectNamedPipe(named_pipe)

  except:

  pass

  client.py

  import win32pipe, win32fileimport time

  PIPE_NAME = r'\\\\.\\pipe\\test_pipe'

  file_handle = win32file.CreateFile(PIPE_NAME,

  win32file.GENERIC_READ | win32file.GENERIC_WRITE,

  win32file.FILE_SHARE_WRITE, None,

  win32file.OPEN_EXISTING, 0, None)try:

  for i in range(1, 11):

  msg = str(i)

  print 'send msg:', msg

  win32file.WriteFile(file_handle, msg)

  time.sleep(1)finally:

  try:

  win32file.CloseHandle(file_handle)

  except:

  pass

  測(cè)試

 ?。?nbsp;首先運(yùn)行server.py

 ?。?nbsp;然后運(yùn)行client.py

 

 

來(lái)源:kongxx的專欄


您還未登錄,請(qǐng)先登錄

熱門帖子

最新帖子

?