Sublime Forum

Help to execute this with thread?

#1

Hello I created this plugin

import os, sublime_plugin
class CmdCommand(sublime_plugin.TextCommand):
def run(self, edit):

    file_name_php=self.view.file_name()
    file_name_xml=file_name_php[:-4]
    os.system("php "+file_name_php+" "+file_name_xml+".xml 1")
    os.system(file_name_xml+".xml")

But while it get executed I can’t use sublime because it gets freezed, how can I execute this on a second thread or something like that?

0 Likes

#2

You should use the subprocess module.

Then change it so something like:

import subprocess
# ...
subprocess.Popen("php {0} {1}.xml 1 && {1}.xml".format(file_name_php, file_name_xml), shell=True)
1 Like

#3

Thanks, that works like a charm

Edit: it’s possible to show the console without close it? because if i quit shell=true it doesn’t open the file at end, it doest at the begin

0 Likes

#4

Can you elaborate what you are trying to archive and which OS you are on?

0 Likes

#5

Sure, I’m using windows 10, I want to execute a php file, transform it in to xml and then open the xml when php file finish to execute, also I want to be able to see the console (cmd) because with shell=False it closes when it finish, and with Shell=true it doesn’t even show.

This comand works fine “php {0} {1}.xml 1” but “{1}.xml” doesn’t work if I set the Shell=true, is there anyways to pause the console till I press any key? and also fix the open file issue?

0 Likes

#6

You can also use the built-in build command:

self.view.window().run_command("exec", {"shell_cmd": "php {0} {1}.xml 1 && {1}.xml".format(file_name_php, file_name_xml)})

This will show the output inside sublime text.

Otherwise I would use something like:

subprocess.Popen('start cmd /c "php {0} {1}.xml 1 && {1}.xml && pause"'.format(file_name_php, file_name_xml), shell=True)
3 Likes

#7

That’s what I wanted, thanks :slightly_smiling:

1 Like