Running a part of python code in background
<p>I am writing a python script which provides two options to the user. In one option, the user input is used to run a function in background. In other, the user input is used to run a function in foreground. How can I achieve both? I don't want to use the "nohup" command to run the full script in background. I only want a certain function to run in background.</p>

<p>I also want the background process to stop on user's will.</p>

<p>Here is a small sample of what I want to do:</p>

<pre><code>def display():
    cnt = 1
    a = []
    if len(live_matches) == 0:
        print "sorry, no live matches currently"
    else:
        for match in live_matches:
            print str(cnt) + "." + match['mchdesc'] + "," + match['mnum'] 
            a[cnt] = match
            cnt = cnt + 1
        choice = raw_input("Enter the match numbers for live updates separated by spaces")
        for c in choice.split(' '):
            update_matches.append(a[int(c)])
        if len(update_matches) &gt; 0:
            #call some function and run in background
    cnt = 1
    for match in completed_matches:
        print str(cnt) + "." + match['mchdesc'] + "," + match['mnum'] 
        cnt = cnt + 1
    choice = raw_input("enter the match number for scorecard")
    #call some function again but run it in foreground
</code></pre>

<p>1.<code>threading.Thread</code> maybe help you, and<code>threading.Lock()</code> will lock your data.  </p>

<p>I just have a idea about it,you can use <code>global input data</code> to check user input and two threads will check it,and determine who locks your output data and the main thread will print it,and also the input can end the two threads.  (maybe break the <code>while</code> loop)</p>

<p>2.await/async is a good way for asynchronous IO,you can use <code>send</code> method to execute <em>native co-routine functions</em> until <code>yield</code>.
Maybe it can do this.</p>

<p>Hope this helps you.</p>

python nohup