Spaces:
BG5
/
Sleeping

BG5 commited on
Commit
c58c205
·
verified ·
1 Parent(s): 300f10a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -7
app.py CHANGED
@@ -9,16 +9,15 @@ app = Flask(__name__)
9
  app.config['SECRET_KEY'] = 'your_secret_key'
10
  socketio = SocketIO(app)
11
 
 
 
12
  def run_shell():
13
- master_fd, slave_fd = pty.openpty()
14
  process = subprocess.Popen(['bash'], stdin=slave_fd, stdout=slave_fd, stderr=slave_fd, text=True)
15
-
16
  while True:
17
  output = os.read(master_fd, 1024).decode('utf-8')
18
  if output:
19
  socketio.emit('output', output)
20
-
21
- # 这里可以添加一个小的延迟,避免CPU占用过高
22
  socketio.sleep(0.1)
23
 
24
  @app.route('/')
@@ -27,11 +26,10 @@ def index():
27
 
28
  @socketio.on('input')
29
  def handle_input(input_data):
30
- # 将输入写入伪终端
31
- os.write(shell_master_fd, (input_data + '\n').encode('utf-8'))
32
 
33
  if __name__ == '__main__':
34
- shell_master_fd, shell_slave_fd = pty.openpty()
35
  shell_thread = threading.Thread(target=run_shell)
 
36
  shell_thread.start()
37
  socketio.run(app, host='0.0.0.0', port=7860, allow_unsafe_werkzeug=True)
 
9
  app.config['SECRET_KEY'] = 'your_secret_key'
10
  socketio = SocketIO(app)
11
 
12
+ master_fd, slave_fd = pty.openpty()
13
+
14
  def run_shell():
 
15
  process = subprocess.Popen(['bash'], stdin=slave_fd, stdout=slave_fd, stderr=slave_fd, text=True)
16
+
17
  while True:
18
  output = os.read(master_fd, 1024).decode('utf-8')
19
  if output:
20
  socketio.emit('output', output)
 
 
21
  socketio.sleep(0.1)
22
 
23
  @app.route('/')
 
26
 
27
  @socketio.on('input')
28
  def handle_input(input_data):
29
+ os.write(master_fd, (input_data + '\n').encode('utf-8'))
 
30
 
31
  if __name__ == '__main__':
 
32
  shell_thread = threading.Thread(target=run_shell)
33
+ shell_thread.daemon = True
34
  shell_thread.start()
35
  socketio.run(app, host='0.0.0.0', port=7860, allow_unsafe_werkzeug=True)