-1

enter image description hereI'm writing unit tests for my management commands. I would like to run my two management commands meanwhile by using call_command() function. How can I do that? I need to run them at the same time because they do work together, one creates data and the other uses that data.

Rheawin
  • 94
  • 8
  • 1
    Could you show us the source of these management commands? – Jaap Joris Vens Jan 05 '21 at 22:12
  • It's long but I can describe. One of them is actually checks which status room has at that moment inside the while loop and creates datas for that status . Other also work inside while loop and checks datas and change room status. So briefly there are while loops. If you stil need source code, I can share. – Rheawin Jan 05 '21 at 22:19
  • [Please don't post screenshots of text](https://meta.stackoverflow.com/a/285557/354577). They can't be searched or copied and offer poor usability. Instead, paste the code as text directly into your question. If you select it and click the `{}` button or Ctrl+K the code block will be indented by four spaces, which will cause it to be rendered as code. – ChrisGPT was on strike Jan 05 '21 at 22:29
  • It sounds like maybe neither of your management commands should be a management command. Anyway, we don't need the full source; a [mcve] will do. – ChrisGPT was on strike Jan 05 '21 at 22:30
  • I will pay attention next times thanks for advices, management commands have to be stay as now for temporarily. – Rheawin Jan 05 '21 at 22:39

1 Answers1

1

While I certainly don't think this is the right approach, the correct way two run two things "simultaneously" in Python is by using threads.

The following code will launch two threads, each running their own management command.

from threading import Thread

def function1():
    call_command('fake')

def function2():
    call_command('engine')

thread1 = Thread(target=function1)
thread2 = Thread(target=function2)

thread1.start() # returns immediately
thread2.start() # returns immediately

import time
time.sleep(3600) # now we wait...

A better approach would be to try to create a single management command that does what you need, so threads aren't needed. You will find that using threads, especially as a beginner, will make things needlessly complicated.

Jaap Joris Vens
  • 3,382
  • 2
  • 26
  • 42
  • 1
    thank you very much, that's whats I want. Actually one of the command is temporary, so we will change it in future but thanks for your advice. – Rheawin Jan 05 '21 at 22:36