def test_pipe_read (self): # ensure that 'readline' works properly on GreenPipes when data is not # immediately available (fd is nonblocking, was raising EAGAIN) # also ensures that readline() terminates on '\n' and '\r\n' r, w = os.pipe() r = GreenPipe(r) w = GreenPipe(w, 'w') def writer (): sleep(.1) w.write('line\n') w.flush() w.write('line\r\n') w.flush() gt = spawn(writer) sleep(0) line = r.readline() self.assertEquals(line, 'line\n') line = r.readline() self.assertEquals(line, 'line\r\n') gt.wait()
def test_pipe_read(self): # ensure that 'readline' works properly on GreenPipes when data is not # immediately available (fd is nonblocking, was raising EAGAIN) # also ensures that readline() terminates on '\n' and '\r\n' r, w = os.pipe() r = GreenPipe(r) w = GreenPipe(w, 'w') def writer(): sleep(.1) w.write('line\n') w.flush() w.write('line\r\n') w.flush() gt = spawn(writer) sleep(0) line = r.readline() self.assertEquals(line, 'line\n') line = r.readline() self.assertEquals(line, 'line\r\n') gt.wait()
def test_pipe (self): r, w = os.pipe() rf = GreenPipe(r, 'r'); wf = GreenPipe(w, 'w', 0); def sender (f, content): for ch in content: sleep(0.0001) f.write(ch) f.close() one_line = "12345\n"; spawn(sender, wf, one_line * 5) for i in xrange(5): line = rf.readline() sleep(0.01) self.assertEquals(line, one_line) self.assertEquals(rf.readline(), '')
def test_pipe(self): r, w = os.pipe() rf = GreenPipe(r, 'r') wf = GreenPipe(w, 'w', 0) def sender(f, content): for ch in content: sleep(0.0001) f.write(ch) f.close() one_line = "12345\n" spawn(sender, wf, one_line * 5) for i in xrange(5): line = rf.readline() sleep(0.01) self.assertEquals(line, one_line) self.assertEquals(rf.readline(), '')
def setup(): global _rfile, _wfile, _threads, _coro, _setup_already, _rspq if _setup_already: return else: _setup_already = True try: _rpipe, _wpipe = os.pipe() _wfile = GreenPipe(_wpipe, 'wb', 0) _rfile = GreenPipe(_rpipe, 'rb', 0) except (ImportError, NotImplementedError): # This is Windows compatibility -- use a socket instead of a pipe because # pipes don't really exist on Windows. import socket from evy import util sock = util.__original_socket__(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('localhost', 0)) sock.listen(50) csock = util.__original_socket__(socket.AF_INET, socket.SOCK_STREAM) csock.connect(('localhost', sock.getsockname()[1])) nsock, addr = sock.accept() _rfile = GreenSocket(csock).makefile('rb', 0) _wfile = nsock.makefile('wb', 0) _rspq = Queue(maxsize=-1) assert _nthreads >= 0, "Can't specify negative number of threads" if _nthreads == 0: import warnings warnings.warn( "Zero threads in tpool. All tpool.execute calls will\ execute in main thread. Check the value of the environment \ variable EVENTLET_THREADPOOL_SIZE.", RuntimeWarning) for i in xrange(_nthreads): reqq = Queue(maxsize=-1) t = threading.Thread(target=tworker, name="tpool_thread_%s" % i, args=(reqq, )) t.setDaemon(True) t.start() _threads.append((reqq, t)) _coro = greenthread.spawn_n(tpool_trampoline)
def __init__ (self, args, bufsize = 0, *argss, **kwds): # Forward the call to base-class constructor subprocess_orig.Popen.__init__(self, args, 0, *argss, **kwds) # Now wrap the pipes, if any. This logic is loosely borrowed from # evy.processes.Process.run() method. for attr in "stdin", "stdout", "stderr": pipe = getattr(self, attr) if pipe is not None and not type(pipe) == GreenPipe: wrapped_pipe = GreenPipe(pipe, pipe.mode, bufsize) setattr(self, attr, wrapped_pipe)
def fdopen(fd, *args, **kw): """ fdopen(fd [, mode='r' [, bufsize]]) -> file_object Return an open file object connected to a file descriptor.""" if not isinstance(fd, int): raise TypeError('fd should be int, not %r' % fd) try: return GreenPipe(fd, *args, **kw) except IOError, e: raise OSError(*e.args)
def test_pipe_writes_large_messages (self): r, w = os.pipe() r = GreenPipe(r) w = GreenPipe(w, 'w') large_message = "".join([1024 * chr(i) for i in xrange(65)]) def writer (): w.write(large_message) w.close() gt = spawn(writer) for i in xrange(65): buf = r.read(1024) expected = 1024 * chr(i) self.assertEquals(buf, expected, "expected=%r..%r, found=%r..%r iter=%d" % (expected[:4], expected[-4:], buf[:4], buf[-4:], i)) gt.wait()
def test_pipe_writes_large_messages(self): r, w = os.pipe() r = GreenPipe(r) w = GreenPipe(w, 'w') large_message = "".join([1024 * chr(i) for i in xrange(65)]) def writer(): w.write(large_message) w.close() gt = spawn(writer) for i in xrange(65): buf = r.read(1024) expected = 1024 * chr(i) self.assertEquals( buf, expected, "expected=%r..%r, found=%r..%r iter=%d" % (expected[:4], expected[-4:], buf[:4], buf[-4:], i)) gt.wait()
def test_truncate (self): f = GreenPipe(self.tempdir + "/TestFile", 'w+', 1024) f.write('1234567890') f.truncate(9) self.assertEquals(f.tell(), 9)
def test_seek_on_buffered_pipe (self): f = GreenPipe(self.tempdir + "/TestFile", 'w+', 1024) self.assertEquals(f.tell(), 0) f.seek(0, 2) self.assertEquals(f.tell(), 0) f.write('1234567890') f.seek(0, 2) self.assertEquals(f.tell(), 10) f.seek(0) value = f.read(1) self.assertEqual(value, '1') self.assertEquals(f.tell(), 1) value = f.read(1) self.assertEqual(value, '2') self.assertEquals(f.tell(), 2) f.seek(0, 1) self.assertEqual(f.readline(), '34567890') f.seek(0) self.assertEqual(f.readline(), '1234567890') f.seek(0, 2) self.assertEqual(f.readline(), '')
def test_truncate(self): f = GreenPipe(self.tempdir + "/TestFile", 'w+', 1024) f.write('1234567890') f.truncate(9) self.assertEquals(f.tell(), 9)
def test_seek_on_buffered_pipe(self): f = GreenPipe(self.tempdir + "/TestFile", 'w+', 1024) self.assertEquals(f.tell(), 0) f.seek(0, 2) self.assertEquals(f.tell(), 0) f.write('1234567890') f.seek(0, 2) self.assertEquals(f.tell(), 10) f.seek(0) value = f.read(1) self.assertEqual(value, '1') self.assertEquals(f.tell(), 1) value = f.read(1) self.assertEqual(value, '2') self.assertEquals(f.tell(), 2) f.seek(0, 1) self.assertEqual(f.readline(), '34567890') f.seek(0) self.assertEqual(f.readline(), '1234567890') f.seek(0, 2) self.assertEqual(f.readline(), '')