Re: A thread import problem

Discussion in 'Python' started by Bruce Sherwood, Jul 21, 2012.

  1. I couldn't get a simple test case to work. I append a listing of the
    little test files, all in the same folder. The diagnostic statement
    print('after start_new_thread\n') works, but then nothing. Originally
    I tried importing testABA.py but was worried that the circular
    importing (A imports B which imports A) would be a problem, hence the
    test of importing a version of the test program without the import.

    The failure of this test case suggests that one cannot do imports
    inside secondary threads started in imported modules, something I keep
    tripping over. But I hope you'll be able to tell me that I'm doing
    something wrong!

    Incidentally, a simple test is to execute the file ABA.py, in which
    case everything works.

    Bruce Sherwood

    ---------------------------
    testABA.py -- execute this file

    from ABA import *
    print('exec testABA')
    from math import sin
    print(sin(3.14159/6))

    ----------------------------
    testABA_noimport.py -- a version of testABA.py without the import of ABA

    print('exec testABA_noimport')
    from math import sin
    print(sin(3.14159/6))

    -----------------------------
    ABA.py

    from thread import start_new_thread
    from time import sleep
    import sys

    user = 'testABA_noimport'
    start_new_thread(lambda: __import__(user), ())
    print('after start_new_thread\n')

    while True:
    sleep(1)

    On Sat, Jul 21, 2012 at 2:32 AM, Dieter Maurer <> wrote:
    > The usual approach to this situation is to invoke the user code via
    > a callback from the UI main loop or invoke it explicitely
    > after the UI system has been set up immediately before its main loop
    > is called. Might look somehow like this:
    >
    > main thread:
    >
    > from thread import start_new_thread
    > from visual import setup_gui, start_main_loop
    > setup_gui() # sets up the GUI subsystem
    > start_new_thread(lambda: __import__(<your module>), ())
    > start_main_loop()
     
    Bruce Sherwood, Jul 21, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Seefeld
    Replies:
    3
    Views:
    999
  2. per9000
    Replies:
    7
    Views:
    426
    Magnus Lycka
    Feb 27, 2006
  3. Bruce Sherwood

    A thread import problem

    Bruce Sherwood, Jul 19, 2012, in forum: Python
    Replies:
    0
    Views:
    167
    Bruce Sherwood
    Jul 19, 2012
  4. Dennis Lee Bieber

    Re: A thread import problem

    Dennis Lee Bieber, Jul 19, 2012, in forum: Python
    Replies:
    0
    Views:
    191
    Dennis Lee Bieber
    Jul 19, 2012
  5. Dieter Maurer

    Re: A thread import problem

    Dieter Maurer, Jul 19, 2012, in forum: Python
    Replies:
    0
    Views:
    159
    Dieter Maurer
    Jul 19, 2012
Loading...

Share This Page