Wookai
Wookai

Reputation: 21793

Spawning more than one thread in Python causes RuntimeError

I'm trying to add multithreading to a Python app, and thus started with some toy examples :

import threading

def myfunc(arg1, arg2):
     print 'In thread'
     print 'args are', arg1, arg2

thread = threading.Thread(target=myfunc, args=('asdf', 'jkle'))

thread.start()
thread.join()

This works beautifully, but as soon as I try to start a second thread, I get a RuntimeError :

import threading

def myfunc(arg1, arg2):
     print 'In thread'
     print 'args are', arg1, arg2

thread = threading.Thread(target=myfunc, args=('asdf', 'jkle'))
thread2 = threading.Thread(target=myfunc, args=('1234', '3763763é'))

thread.start()
thread2.start()

thread.join()
thread2.join()

As others seems to have no problem running this code, let me add that I am on Windows 7 x64 Pro with Python 2.6.3 32bits (if that matters).

Upvotes: 0

Views: 532

Answers (4)

Pinglei
Pinglei

Reputation: 1

Maybe it is because you have the same filename or project name like "threading" or "Thread" under some directory and you have runned it once since this bootup.

Upvotes: 0

Wookai
Wookai

Reputation: 21793

As said in the comments, I think that the problem comes from IDLE itself, and not from my code. Thanks for your help anyway !

I upvoted your answers but will be accepting mine, as there is no real solution to this problem.

Upvotes: 0

matt b
matt b

Reputation: 140041

Can you post the exact error you get?

Runs fine for me (after replacing the é character with an e):

In thread
args areIn thread
asdfargs are  jkle1234
 3763763e

If I leave the original script you posted and save the file as UTF-8 with BOM on Windows:

In thread
args areIn thread
asdfargs are  jkle1234
 3763763é

Saving the code you posted as ASCII results in a SyntaxError:

SyntaxError: Non-ASCII character '\xe9' in file threadtest.py on line 8, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

Environment information:

C:\python -V
Python 2.6.2
C:\cmd
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

Upvotes: 1

Jeff Ober
Jeff Ober

Reputation: 5027

thread2 = threading.Thread(target=myfunc, args=('1234', '3763763é'))

Are you declaring the file as UTF-8?-----------------------------------------------------^

Upvotes: 1

Related Questions