Пример #1
0
	def __init__(self, cmd, buffer_length=256):
		newenv = {
			"PATH": "/bin:/sbin:/usr/bin:/usr/sbin:/home/patxi/bin:/usr/local/bin:/usr/local/sbin",
			"HOME": "/home/patxi",
			"TERM": "xterm",
			"USERNAME": "******",
			"SHELL": "/bin/bash"
		}
		self.proc = PopenPty(cmd, env=newenv)
		self.running = self.proc.poll() is None
		self.data = []
		self.max = buffer_length
		self.ridx = 0
		self.widx = 0
		gevent.spawn(self._gthread_loop)
Пример #2
0
class Process(object):
	def __init__(self, cmd, buffer_length=256):
		newenv = {
			"PATH": "/bin:/sbin:/usr/bin:/usr/sbin:/home/patxi/bin:/usr/local/bin:/usr/local/sbin",
			"HOME": "/home/patxi",
			"TERM": "xterm",
			"USERNAME": "******",
			"SHELL": "/bin/bash"
		}
		self.proc = PopenPty(cmd, env=newenv)
		self.running = self.proc.poll() is None
		self.data = []
		self.max = buffer_length
		self.ridx = 0
		self.widx = 0
		gevent.spawn(self._gthread_loop)

	def _gthread_loop(self):
		poll = self.proc.poll()
		while poll is None:
			data = self.proc.stdout.readline().decode('utf-8')
			#print("Process._gthread_loop: got %s" % repr(data))
			if data:
				if self.widx < self.max:
					self.data.append(data)
				else:
					self.data[self.widx % self.max] = data
					self.ridx += 1
				self.widx += 1
			poll = self.proc.poll()
		self.running = False
		print(u'Command exited with code %d' % poll)
		if self.widx < self.max:
			self.data.append(u'Command exited with code %d' % poll)
		else:
			self.data[self.widx % self.max] = u'Command exited with code %d' % poll
			self.ridx += 1
		self.widx += 1
		self.proc.wait()

	def write(self, data):
		if self.proc.poll() is None:
			self.proc.stdin.write(data)

	def __len__(self):
		if self.empty: return 0
		elif self.widx > self.ridx: return self.widx - self.ridx
		else: return self.max

	def __getitem__(self, idx):
		if not isinstance(idx, (int, long)): raise TypeError("Not an integer index")
		if idx < 0:
			raise IndexError("Out of index")
		idx %= self.max
		return self.data[idx]