Python - User input to CGI via. Threading and reading file

橙三吉。 提交于 2019-12-06 00:06:28

A CGI script is a pretty primitive operation. It works basically the same as any normal script you run from your command shell. An http request is made to the web server. The server starts a new process and passes the arguments in via stdin to the script. At this point, it's like a normal script.

A script can't get any more input unless it's looking for input by some means, so you are correct in assuming that once the headers are sent, the web client can no longer directly send more input, because the request is already in progress, and the response is already in progress as well.

A thread watching a file is one way to introduce a control loop to the script. Another is to open a UNIX socket to a path based on your unique ID for each instance. Then have the thread sitting on the socket for input. What you would then have to do is pass the ID back to the web client. And the client could make a call to the second script with the ID, which would then know the proper UNIX socket path to send control commands to: ie.

/tmp/script-foo/control/<id>.socket

You actually might only need 1 thread. You main thread could simply loop over checking for information on the socket, and monitoring the current operation being run in a thread or subprocess. It might be like this in pseudocode:

uid = generate_unique_id()
sock = socket.socket(AF_UNIX)
sock.bind('/tmp/script-foo/control/%s.socket' % uid)
# and set other sock options like timeout

taskList = [a,b,c]
for task in taskList:
    runningTask = start task in thread/process
    while runningTask is running:
        if new data on socket, with timeout N ms
            if command == restart:
                kill runningTask
                taskList = [a,b,c]
                break
            else:
                process command

When the web client sends a command via ajax to your second script, it might look like this in pseudocode:

jobid = request.get('id')
cmd = request.get('cmd')
sock = socket.socket(socket.AF_UNIX)
sock.connect('/tmp/script-foo/control/%s.socket' % jobid)
sock.sendall(cmd)
sock.close()

Update

Based on your code update, here is a working example of what I was suggesting:

import sys
import os
import socket
import uuid 
import time 

# Options
TASKS_DIR = "."

def main():

    sessionId = str(uuid.uuid4())

    print 'Session ID: '+ sessionId
    sys.stdout.write ('<br /><a href="cgi_send.py?cmd=test&session=' + sessionId +'" target="_blank">Send test command</a>')
    sys.stdout.flush()

    address = os.path.join(TASKS_DIR, '%s.socket' % sessionId)

    sock = socket.socket(socket.AF_UNIX)
    sock.setblocking(0)
    sock.settimeout(.1)
    sock.bind(address)
    sock.listen(1)


    fakeTasks = [foo_task, foo_task, foo_task]

    try:
        for task in fakeTasks:

            # pretend we started a task
            runningTask = task()
            # runningTask = Thread(target=task) 
            # runningTask.start()

            # while runningTask.is_alive():   
            while runningTask:
                conn = None
                try:
                    conn, addr = sock.accept()
                    data = conn.recv(100).strip()

                except socket.timeout:
                    # nothing ready from a client
                    continue

                except socket.error, e:
                    print "<br />Connection Error from client"

                else:
                    print "<br />"+ data
                    sys.stdout.flush()
                    conn.close()

                    # for the thread version, you will need some 
                    # approach to kill or interrupt it. 
                    # This is just simulating. 
                    if data == "CANCEL":
                        # temp way to cancel our task
                        print "<br />Cancelling current task." 
                        runningTask = False

                    elif data == "QUIT":
                        print "<br />Quitting entire process." 
                        runningTask = False 
                        fakeTasks[:] = []

                finally:
                    if conn:
                        conn.close()

    finally:
        sock.close()
        os.remove(address)



def foo_task():
    print 'foo task'
    return True


if __name__ == '__main__':
    sys.stdout.write("Content-type:text/html;charset=utf-8\r\n\r\n")
    sys.stdout.write('<!DOCTYPE html>\n<html><head><title>Test</title></head><body>')

    main()

    print '</body></html>'
    sys.exit()

Instead of using a 10 second global timeout, you set it to something small like 100ms. It loops over each task and starts it (eventually in a thread), and then tries to loop over waiting for a socket connection. If there is no connection within 100ms, it will timeout and continue to loop, while checking if the task is done. At any point, a client can connect and issue either a "CANCEL" or "QUIT" command. The socket will accept the connection, read it, and react.

You can see how you do not need multiple threads here for the solution. The only threading or subprocess you need is to run the task.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!