Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

[solved] Using thread

edited July 2012 in OpenSesame

Hi Sebastian.

I have a problem with this code that uses thread to produce a windows beep. I run OpenSesame from source, in order to avoid modules problems, but the problem is the same, irrespective of the installation.

import thread import winsound def windowsBeep(): lock = thread.allocate_lock() lock.acquire() winsound.Beep(1000, 100) lock.release() # ... # ... # subsequently, in a part of the code... thread.start_new_thread(windowsBeep,()) # produce a beep

The debug window doesn't return any error, simply the beep doesn't start. However, the cmd window returns this error:

Unhandles exception in thread started by Traceback (most recent call last): file "(string)", line 44, in windowsBeep NameError: global name 'thread' is not defined

I noticed that, inside OpenSesame, you have to define global variables if you want to use them inside a function. Is the problem due to this aspect?

PS. If I use the winsound command, without the thread, the beep starts, but it doesn't works in as I would want, so the thread is important for me.

Do you have any suggestion?




  • edited 11:05PM

    Hi Andrea,

    Yes, this is because what you're really doing is defining a function within a function, because the script is in itself already the body of a function. So in practice your script would look like this:

    def run()
        import thread
        import winsound
        def windowsBeep():
            lock = thread.allocate_lock()
            winsound.Beep(1000, 100)

    Does that make sense? It's not very intuitive, I know.

    Because of this structure it gets a bit awkward when you're defining functions, having to make variables global and re-importing modules in each function.

    So the solution would be to add the import statement also to the function. Or you could use the external_script plug-in, in which case you can use a Python script with real functions etc (see plug-in documentation).


    Buy Me A Coffee

  • edited 11:05PM

    Many many thanks, Sebastian. This solves the problem. :)



  • edited 11:05PM

    Related to the use of functions in OpenSesame, if I want to define a function that I have to repeatedly use in (say) four different inline scripts, how can I write that function only one time at the begin of the experiment, making it callable in the various scripts?

  • edited 11:05PM

    Good question!

    Quite amazingly, you can treat functions pretty much like variables in Python. So you could make a function globally available either by adding as a property to the experiment object, or by making it global. Both methods are identical to what you could do with a variable.

    Define your functions in one inline_script:

    # Method 1: Add function as property to experiment
    def func1():
        print "I'm a property!"
    exp.func1 = func1
    # Method 2: Make function global
    global func2
    def func2():
        print "I'm global!"

    Use them in another inline_script:


    Buy Me A Coffee

  • edited 11:05PM

    Perfect! I think that defining variables and functions as global is more easy, at least when one has to use a lot of objects.

    Thanks again.

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games