1010import msvcrt
1111import os
1212import subprocess
13- import tempfile
1413import warnings
1514
1615
2423PIPE = subprocess .PIPE
2524STDOUT = subprocess .STDOUT
2625_mmap_counter = itertools .count ()
26+ _MAX_PIPE_ATTEMPTS = 20
2727
2828
2929# Replacement for os.pipe() using handles instead of fds
3030
3131
3232def pipe (* , duplex = False , overlapped = (True , True ), bufsize = BUFSIZE ):
3333 """Like os.pipe() but with overlapped support and using handles not fds."""
34- address = tempfile .mktemp (
35- prefix = r'\\.\pipe\python-pipe-{:d}-{:d}-' .format (
36- os .getpid (), next (_mmap_counter )))
37-
3834 if duplex :
3935 openmode = _winapi .PIPE_ACCESS_DUPLEX
4036 access = _winapi .GENERIC_READ | _winapi .GENERIC_WRITE
@@ -56,9 +52,20 @@ def pipe(*, duplex=False, overlapped=(True, True), bufsize=BUFSIZE):
5652
5753 h1 = h2 = None
5854 try :
59- h1 = _winapi .CreateNamedPipe (
60- address , openmode , _winapi .PIPE_WAIT ,
61- 1 , obsize , ibsize , _winapi .NMPWAIT_WAIT_FOREVER , _winapi .NULL )
55+ for attempts in itertools .count ():
56+ address = r'\\.\pipe\python-pipe-{:d}-{:d}-{}' .format (
57+ os .getpid (), next (_mmap_counter ), os .urandom (8 ).hex ())
58+ try :
59+ h1 = _winapi .CreateNamedPipe (
60+ address , openmode , _winapi .PIPE_WAIT ,
61+ 1 , obsize , ibsize , _winapi .NMPWAIT_WAIT_FOREVER , _winapi .NULL )
62+ break
63+ except OSError as e :
64+ if attempts >= _MAX_PIPE_ATTEMPTS :
65+ raise
66+ if e .winerror not in (_winapi .ERROR_PIPE_BUSY ,
67+ _winapi .ERROR_ACCESS_DENIED ):
68+ raise
6269
6370 h2 = _winapi .CreateFile (
6471 address , access , 0 , _winapi .NULL , _winapi .OPEN_EXISTING ,
0 commit comments