[solved] Using thread
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?
Thanks,
Andrea

Comments
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() lock.acquire() winsound.Beep(1000, 100) lock.release()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).
Cheers!
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Many many thanks, Sebastian. This solves the problem.
Best,
Andrea
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?
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:
Check out SigmundAI.eu for our OpenSesame AI assistant!
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.