0

I am using this toolkit: https://github.com/Fire30/Fifa14Client in python 2.7

from Fifa14Client import LoginManager
from Fifa14Client import WebAppFunctioner
import ConfigParser
from extra import EAHash
import threading


def do_main():
    Config = ConfigParser.ConfigParser()
    Config.read("accounts_example.ini")
    for section in Config.sections():
        email = Config.get(section, 'Email')
        password = Config.get(section, 'Password')
        secret_answer = Config.get(section, 'Secret')
        security_hash = EAHash.EAHashingAlgorithm().EAHash(secret_answer)
        platform = Config.get(section, 'Platform')

        login = LoginManager.LoginManager(email,password,security_hash,platform)
        login.login()
        func = WebAppFunctioner.WebAppFunctioner(login)

What this part of my script does is access an ini file with the login details of accounts for a website and logs in. There are seperate sections, each with their own accounts in the ini file which look like this:

[AccountOne]

Email:xxx@xxx.com

Password:qwerty123

Secret:answer

Platform:xbox

[AccountTwo]

Email:xxx@xxx.com

Password:qwerty123

Secret:answer

Platform:xbox

And so on. What I want to do is assign a thread to each account so each of them has a separate thread

Sorry if I'm being a little unclear and thanks in advance

  • 1
    Well, looks like you know you need the `threading` module. Have you tried actually using it in your code? If so can you share what you've tried? – dano Jun 28 '14 at 14:48
  • I've assigned the accounts to one thread, but the problem is that they all get assigned to just one thread – user3785989 Jun 28 '14 at 14:53

1 Answers1

0

You can simply use threading.Thread for this:

from Fifa14Client import LoginManager
from Fifa14Client import WebAppFunctioner
import ConfigParser
from extra import EAHash
import threading


def login_thread(email, password, security_hash, platform):
    # This function runs in a separate thread.
    login = LoginManager.LoginManager(email,password,security_hash,platform)
    login.login()
    func = WebAppFunctioner.WebAppFunctioner(login)
    # Do more stuff here

def do_main():
    Config = ConfigParser.ConfigParser()
    Config.read("accounts_example.ini")
    threads = []
    for section in Config.sections():
        email = Config.get(section, 'Email')
        password = Config.get(section, 'Password')
        secret_answer = Config.get(section, 'Secret')
        security_hash = EAHash.EAHashingAlgorithm().EAHash(secret_answer)
        platform = Config.get(section, 'Platform')
        t = threading.Thread(target=login_thread, 
                             args=(email, password, security_hash, platform))
        t.start()
        threads.append(t)

    for t in threads: # Wait for all the threads to finish
        t.join()

Keep in mind that because of the Global Interpreter Lock (GIL), threads in Python cannot run concurrently across CPU cores, so you won't achieve true parallelism. To get that, you should use the multiprocessing library, which has an API almost identical to threading, but isn't limited by the GIL; it uses sub-processes for parallelism, rather than threads. The only change required in the above code to make the switch would be to use multiprocessing.Process instead of threading.Thread.

Community
  • 1
  • 1
dano
  • 91,354
  • 19
  • 222
  • 219