Search code examples
python-3.xtorstem

multiple Tor instances through stem


I am developing a script that creates 3 Tor instances using stem, from stem tutorial "to Russia with love".

def print_bootstrap_lines(line):
  if "Bootstrapped " in line:
    print(term.format(line, term.Color.BLUE))

def main():
        SocksPort=9050
        #print(str(SocksPort))
        i=0
        while i<2:
                tor_process=stem.process.launch_tor_with_config(
                config={
                'SocksPort':str(SocksPort),
                'ControlPort':str(SocksPort+1),
                'ExitNodes':'{ru}',
                'StrictNodes':'1',},
                init_msg_handler=print_bootstrap_lines,
                )
                SocksPort=SocksPort+2
                i=i+1

after the creation of the first instance, a return error is printed:

OSError: Process terminated: No, it's still there.  Exiting.

Solution

  • launch_tor_with_config() function does launch tor with a temporary new torrc file which gets deleted once the tor instance is killed/stopped. but i noticed that there is a lock file, like the one you have when you interrupt the Linux package manager updates, but it is located in ~/.tor/lck so before starting the new instance make sure to wait for the lock file to appear, then delete it from within your script, and the multiple tor instances will be created successfully.