Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I make a fork or non-blocking system call in python

This is related to this question but with a different take.

In Ubuntu, I use Autokey, which uses python to automate keystrokes it observes. So I have <super>+e mapped to open Gedit, <shift>+<super>+3 to open OOwriter, etc etc. When I make one of these calls, I cannot make another one until the previous program called has exited.

Here's a sample of the script it executes:

import subprocess
subprocess.call("/opt/openoffice.org3/program/scalc")

... same behavior using:

import os
os.system("/opt/openoffice.org3/program/scalc")

This all worked smoothly in my previous Ubuntu 10.04LTS, but things have changed since then and I can't repeated make these calls.

Would you please help me with how to fork or do something to "get back" from that subprocess.call() without waiting for the program to exit? I tried nohup and backgrounding /opt/openoffice.org3/program/scalc & but those do nothing (probably breaks something in Autokey and Py)


Answer: the answer below didn't actually work, but got me snooping around more and I found another SO answer which did work for my situation!

#Enter script code -- mapped to <super>+e
import thread
thread.start_new_thread(os.system,('gedit',))

This totally worked!! I can hit <super>+e 2 or 3 times in a row and it keeps adding tabs to gedit. :) This script makes Autokey act as though the command in quotes is typed at the command line.

like image 611
Krista K Avatar asked Dec 16 '22 22:12

Krista K


1 Answers

It's as easy as using Popen instead of call:

import subprocess
subprocess.Popen("/opt/openoffice.org3/program/scalc")

call should never have worked that way since it has to return the exit code, meaning the program would actually have to exit.

like image 120
Dan Avatar answered Mar 24 '23 01:03

Dan