0

So i have a number of scripts that do different things. I am currently creating a kind of "parent script" that will open the scripts depending on the user input.

This part works very well. I worry that because nothing really happens for a few moments until the desired script displays the welcome(): i have at the start, people may end up exiting the script.

what currently happens:

  1. user enters -create
  2. Welcome to creation script!

there is a decent time gap between 1 and 2. I believe this to be the case because subprocess.Popen OR because of the script that is loading is just taking some time to load up


What i would like to do is after the user enters the script they want to use.

  1. user enters -create

  2. Loading bar (this loading bar can literally be anything animated to show that something is happening but I want it to end when the creation script loads).

  3. Welcome to creation script!

CODE:

def help_list():

    print('\n' '> create  -creates a new list with new or existing dns hosts ')
    print('> add  -adds new or existing dns hosts and or target domains to an existing list ')
    print('> remove  -removes existing dns hosts and or target domains from an existing list ')
    print('> decomm  -deletes an existing list. Only use this command for ticket type "Decomm - proxy"' '\n')

def create_list():

    cmd = ('py C:\\Users\\chuck\\Desktop\\Scripts\\Creation_script.py')
    p = subprocess.Popen(cmd, shell=True)   
    out, err = p.communicate()

def clear_screen():
    os.system("cls")

if __name__ == '__main__':
    clear_screen()
    print('\n' 'enter h or help for a list of options')
    while True:    
        try:
            question = input('\n' 'please enter script to run > ')
            if question == 'help':
                clear_screen()
                help_list()
            if question == 'create':
                clear_screen()
                create_list()
        except:
            break

I cut out some of the other inputs to make this code a little smaller for the example.

I was reading about tqdm, time, sleep, but I am just unsure how to really use any of those to display the progress of opening

::EDIT:: Here's the cProfile. (IDK what any of this means)

         130653 function calls (125870 primitive calls) in 3.232 seconds
  Ordered by: standard name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    1    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:1002(_gcd_import)
63/35    0.000    0.000    0.070    0.002 <frozen importlib._bootstrap>:1017(_handle_fromlist)
  220    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:103(release)
  197    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:143(__init__)
  197    0.000    0.000    0.001    0.000 <frozen importlib._bootstrap>:147(__enter__)
  197    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:151(__exit__)
  220    0.000    0.000    0.001    0.000 <frozen importlib._bootstrap>:157(_get_module_lock)
  198    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:176(cb)
   23    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:194(_lock_unlock_module)
228/3    0.000    0.000    0.127    0.042 <frozen importlib._bootstrap>:211(_call_with_frames_removed)
 2121    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:222(_verbose_message)
   15    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:232(_requires_builtin_wrapper)
  198    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:342(__init__)
  159    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:35(_new_module)
  326    0.000    0.000    0.002    0.000 <frozen importlib._bootstrap>:376(cached)
  425    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:389(parent)
  182    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:397(has_location)
19/15    0.000    0.000    0.018    0.001 <frozen importlib._bootstrap>:406(spec_from_loader)
  182    0.001    0.000    0.003    0.000<frozen importlib._bootstrap>:477(_init_module_attrs)
  182/181    0.000    0.000    0.011    0.000 <frozen importlib._bootstrap>:549(module_from_spec)
  198    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:58(__init__)
    4    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:613(_load_backward_compatible)
186/3    0.000    0.000    0.127    0.042 <frozen importlib._bootstrap>:650(_load_unlocked)
  194    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:725(find_spec)
   15    0.000    0.000    0.001    0.000 <frozen importlib._bootstrap>:746(create_module)
   15    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:754(exec_module)
   15    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:771(is_package)
  220    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:78(acquire)
  179    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:800(find_spec)
  564    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:863(__enter__)
  564    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:867(__exit__)
   12    0.000    0.000    0.018    0.001 <frozen importlib._bootstrap>:881(_find_spec_legacy)
  194/165    0.001    0.000    0.045    0.000 <frozen importlib._bootstrap>:890(_find_spec)
    1    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:937(_sanity_check)
  196/3    0.000    0.000    0.129    0.043 <frozen importlib._bootstrap>:956(_find_and_load_unlocked)
  197/3    0.001    0.000    0.129    0.043 <frozen importlib._bootstrap>:986(_find_and_load)
  159    0.000    0.000    0.005    0.000 <frozen importlib._bootstrap_external>:1010(path_stats)
   14    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:104(_path_isdir)
    8    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:1088(__init__)
   8/7    0.000    0.000    0.007    0.001 <frozen importlib._bootstrap_external>:1099(create_module)
    8    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:1107(exec_module)
   14    0.000    0.000    0.001    0.000 <frozen importlib._bootstrap_external>:1252(_path_hooks)
  461    0.000    0.000    0.001    0.000 <frozen importlib._bootstrap_external>:1265(_path_importer_cache)
  179    0.001    0.000    0.030    0.000 <frozen importlib._bootstrap_external>:1302(_get_spec)
  179    0.000    0.000    0.030    0.000 <frozen importlib._bootstrap_external>:1334(find_spec)
   14    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:1394(__init__)
  112    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:1400(<genexpr>)
  167    0.000    0.000    0.001    0.000 <frozen importlib._bootstrap_external>:1426(_get_spec)
  398    0.002    0.000    0.028    0.000 <frozen importlib._bootstrap_external>:1431(find_spec)
   14    0.000    0.000    0.002    0.000 <frozen importlib._bootstrap_external>:1479(_fill_cache)
   14    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:1508(<setcomp>)
   14    0.000    0.000    0.001    0.000 <frozen importlib._bootstrap_external>:1520(path_hook_for_FileFinder)
  318    0.001    0.000    0.002    0.000 <frozen importlib._bootstrap_external>:294(cache_from_source)
  398    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:36(_relax_case)
  167    0.000    0.000    0.001    0.000 <frozen importlib._bootstrap_external>:424(_get_cached)
  161/160    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:456(_check_name_wrapper)
  159    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:493(_classify_pyc)
  339    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:51(_unpack_uint32)
   90    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:526(_validate_timestamp_pyc)
   69    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:554(_validate_hash_pyc)
  159    0.000    0.000    0.009    0.000 <frozen importlib._bootstrap_external>:578(_compile_bytecode)
 1982    0.001    0.000    0.003    0.000 <frozen importlib._bootstrap_external>:62(_path_join)
  167    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:629(spec_from_file_location)
 1982    0.001    0.000    0.001    0.000 <frozen importlib._bootstrap_external>:64(<listcomp>)
  321    0.001    0.000    0.001    0.000 <frozen importlib._bootstrap_external>:68(_path_split)
    1    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:766(is_package)
  159    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:774(create_module)
  159/3    0.000    0.000    0.127    0.042 <frozen importlib._bootstrap_external>:777(exec_module)
  781    0.000    0.000    0.027    0.000 <frozen importlib._bootstrap_external>:80(_path_stat)
  159    0.001    0.000    0.036    0.000 <frozen importlib._bootstrap_external>:849(get_code)
  210    0.000    0.000    0.007    0.000 <frozen importlib._bootstrap_external>:90(_path_is_mode_type)
  159    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:939(__init__)
  160    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:964(get_filename)
  228    0.002    0.000    0.018    0.000 <frozen importlib._bootstrap_external>:969(get_data)
    1    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:980(get_resource_reader)
  196    0.000    0.000    0.007    0.000 <frozen importlib._bootstrap_external>:99(_path_isfile)
    1    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:990(resource_path)
    1    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:996(is_resource)
   14    0.000    0.000    0.001    0.000 <frozen zipimport>:63(__init__)
    1    0.000    0.000    0.000    0.000 <string>:1(<module>)
   13    0.000    0.000    0.000    0.000 <string>:1(__new__)
    1    0.000    0.000    3.232    3.232 Sophos_DNS_Host_Creation_POC_2.py:1(<module>)
    1    0.000    0.000    3.102    3.102 Sophos_DNS_Host_Creation_POC_2.py:12(site_login)
    1    0.000    0.000    0.001    0.001 Sophos_DNS_Host_Creation_POC_2.py:9(welcome)
    1    0.000    0.000    0.000    0.000 __future__.py:1(<module>)
    1    0.000    0.000    0.000    0.000 __future__.py:81(_Feature)
   10    0.000    0.000    0.000    0.000 __future__.py:83(__init__)
   10    0.000    0.000    0.129    0.013 __init__.py:1(<module>)
    1    0.000    0.000    0.000    0.000 __init__.py:1042(StreamHandler)
    1    0.000    0.000    0.000    0.000 __init__.py:109(import_module)
    1    0.000    0.000    0.000    0.000 __init__.py:1125(FileHandler)
    1    0.000    0.000    0.000    0.000 __init__.py:1194(_StderrHandler)
    1    0.000    0.000    0.000    0.000 __init__.py:1200(__init__)
    1    0.000    0.000    0.000    0.000 __init__.py:1218(PlaceHolder)
    2    0.000    0.000    0.000    0.000 __init__.py:1224(__init__)
    4    0.000    0.000    0.000    0.000 __init__.py:1230(append)
    1    0.000    0.000    0.000    0.000 __init__.py:1260(Manager)
    1    0.000    0.000    0.000    0.000 __init__.py:1265(__init__)
    1    0.000    0.000    0.000    0.000 __init__.py:1280(disable)
    7    0.000    0.000    0.000    0.000 __init__.py:1284(getLogger)
    7    0.000    0.000    0.000    0.000 __init__.py:1335(_fixupParents)
    1    0.000    0.000    0.000    0.000 __init__.py:1359(_fixupChildren)
    1    0.000    0.000    0.000    0.000 __init__.py:1389(Logger)
    8    0.000    0.000    0.000    0.000 __init__.py:1404(__init__)
    2    0.000    0.000    0.000    0.000 __init__.py:1601(addHandler)
    1    0.000    0.000    0.007    0.007 __init__.py:17(<module>)
    1    0.000    0.000    0.000    0.000 __init__.py:1743(RootLogger)
    1    0.000    0.000    0.000    0.000 __init__.py:1749(__init__)
    1    0.000    0.000    0.000    0.000 __init__.py:1760(LoggerAdapter)
    1    0.000    0.000    0.013    0.013 __init__.py:19(<module>)
   12    0.000    0.000    0.000    0.000 __init__.py:193(_checkLevel)
    7    0.000    0.000    0.000    0.000 __init__.py:2018(getLogger)
   59    0.000    0.000    0.000    0.000 __init__.py:21(__new__)
    1    0.000    0.000    0.000    0.000 __init__.py:2147(NullHandler)
    2    0.000    0.000    0.000    0.000 __init__.py:2163(createLock)
   12    0.000    0.000    0.000    0.000 __init__.py:218(_acquireLock)
   12    0.000    0.000    0.000    0.000 __init__.py:227(_releaseLock)
    1    0.000    0.000    0.000    0.000 __init__.py:238(_register_at_fork_reinit_lock)
    1    0.000    0.000    0.000    0.000 __init__.py:276(LogRecord)
   13    0.000    0.000    0.001    0.000 __init__.py:313(namedtuple)
   95    0.000    0.000    0.000    0.000 __init__.py:385(<genexpr>)
    1    0.000    0.000    0.001    0.001 __init__.py:417(PercentStyle)
    1    0.000    0.000    0.000    0.000 __init__.py:424(__init__)
    1    0.000    0.000    0.000    0.000 __init__.py:43(normalize_encoding)
    1    0.000    0.000    0.000    0.000 __init__.py:430(validate)
    1    0.000    0.000    0.001    0.001 __init__.py:445(StrFormatStyle)
    1    0.000    0.000    0.000    0.000 __init__.py:475(StringTemplateStyle)
    1    0.000    0.000    0.000    0.000 __init__.py:49(check_compatibility)
    1    0.000    0.000    0.000    0.000 __init__.py:5(<module>)
    1    0.000    0.000    0.000    0.000 __init__.py:5(HTTPStatus)
    1    0.000    0.000    0.000    0.000 __init__.py:514(Formatter)
    1    0.000    0.000    0.000    0.000 __init__.py:559(__init__)
    1    0.000    0.000    0.000    0.000 __init__.py:692(BufferingFormatter)
    1    0.000    0.000    0.000    0.000 __init__.py:70(search_function)
    1    0.000    0.000    0.000    0.000 __init__.py:734(Filter)
    1    0.000    0.000    0.000    0.000 __init__.py:771(Filterer)
   11    0.000    0.000    0.000    0.000 __init__.py:776(__init__)
    1    0.000    0.000    0.126    0.126 __init__.py:8(<module>)
    3    0.000    0.000    0.000    0.000 __init__.py:843(_addHandlerRef)
    1    0.000    0.000    0.000    0.000 __init__.py:853(Handler)
    3    0.000    0.000    0.000    0.000 __init__.py:862(__init__)
    1    0.000    0.000    0.000    0.000 __init__.py:891(createLock)
    1    0.000    0.000    0.000    0.000 __version__.py:5(<module>)
    1    0.000    0.000    0.000    0.000 _appengine_environ.py:1(<module>)
    1    0.000    0.000    0.000    0.000 _appengine_environ.py:12(is_appengine_sandbox)
    1    0.000    0.000    0.000    0.000 _appengine_environ.py:22(is_local_appengine)
    1    0.000    0.000    0.000    0.000 _appengine_environ.py:28(is_prod_appengine)
    1    0.000    0.000    0.000    0.000 _appengine_environ.py:8(is_appengine)

'''

avgnetmin
  • 31
  • 3
  • I suggest you profile your script and see what's taking the time before "fixing" it. See [How can you profile a Python script?](https://stackoverflow.com/questions/582336/how-can-you-profile-a-python-script) That said, there are many ways to display a progress bar in Python (and many questions about them here on StackOverflow). – martineau Apr 19 '21 at 20:51
  • @martineau It takes about 10 seconds for my script to start. Is that a pretty long time for a script with 364 lines? If it takes about 10 seconds would it really hurt to just add a loading bar within that time? I get that it sounds like a bandage to a problem but can the script load up any faster? – avgnetmin Apr 19 '21 at 21:14
  • Since you are using `subprocess`, your main process can execute concurrently with the other process which should make displaying a progress bar relatively easy — you can check the status of the child process with `Popen.poll()` in a loop. [Here's](https://stackoverflow.com/a/23630948/355230) an outline of how that would work. – martineau Apr 19 '21 at 21:27
  • Not really knowing how long it's going to take will make displaying a progress bar difficult since at and moment you don't really know what percentage is done — so instead consider displaying a simple "spinner". They're really easy to implement and there a number of questions here about them if you search for the term. – martineau Apr 19 '21 at 21:52

0 Answers0