Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Threading problems in Python Interpreter #1116

Closed
jbelusic opened this issue May 7, 2021 · 7 comments
Closed

Threading problems in Python Interpreter #1116

jbelusic opened this issue May 7, 2021 · 7 comments

Comments

@jbelusic
Copy link

jbelusic commented May 7, 2021

This is script to be executed:

import threading
import time

total = 0

def some_computation():
	global total
	for i in range(15):
		total = i
		print("thread i:", i)
		time.sleep(1)
	print('End thread')


def main():
	print("Start")

	thread1 = threading.Thread(target=some_computation)
	#thread1.start()
	#for thread in [thread1]:
	#	thread.join()

	thread1.setDaemon(True)
	thread1.daemon = True
	thread1.start()

	for _ in range(10):
		time.sleep(1)
		print("Total:",total)

	#thread1._stop()

	print("End")

main()
expected output:
Start
thread i: 0
thread i: 1
Total: 1
thread i: 2
Total: 2
thread i: 3
Total: 3
Total: 3
thread i: 4
thread i: 5
Total: 5
Total: 5
thread i: 6
thread i: 7
Total: 7
thread i: 8
Total: 8
Total: 8
thread i: 9
thread i: 10
Total: 10
End

actual output:
Start
thread i: 0
Total: 0
thread i: 1
Total:thread i:  2
1
Total:thread i:  32

thread i:Total: 4
 4
Total:thread i: 5 
4
Total: 5
thread i: 6
thread i:Total: 7
 7
thread i: 8
Total: 8
Total: 8
thread i: 9
thread i: 10Total: 10

End
>>> thread i: 11
thread i: 12
thread i: 13
thread i: 14
End thread
@pyscripter
Copy link
Owner

There are in fact two issues:

  • Line buffering
    Python by default does line buffering of stdout . This means that output is printed when it contains a new line /n. PyScripter v,4 gets the output via redirection using pipes, but in that case python detects that it is not writing to a console and disables line buffering. To get the output PyScripter had to run the python process with -u flag which disables buffering altogether. This is not an issue with single threaded scripts, but it results in the problem which you experienced when different threads print simultaneously. Python 3.7 and later allow you to reconfigure the stdout and this is now implemented and line buffering should be available with Python 3.7 and later. The fix will be available in the next release of PyScripter.

  • daemon threads
    When you run a script with daemon threads, the daemon threads stop when all other threads terminate. However the way PyScripter runs scripts, Python does not terminate when the main thread exits. This has many advantages (post-mortem analysis, allowing you to query the state of variables etc.). A downside is that daemon threads will carry on running until you re-initialize the interpreter, by say running a new script. This is by design, and just a gotcha you need to be aware of. No fix will be provided for this.

Workaround
Assuming that you use python 3.7 or newer a workaround is to include the following at the top of your script:

import sys
sys.stdout.reconfigure(line_buffering=True, write_through=False)

@jbelusic
Copy link
Author

jbelusic commented May 12, 2021 via email

@pyscripter
Copy link
Owner

I can't see the image. You can copy and paste the image in github.

@jbelusic
Copy link
Author

Maybe this is correct behaviour (see thread i 11), as you say that Python interpreter needs to be reinitialized.
Am I correct?

Capture

@jbelusic
Copy link
Author

No matter what, I found this IDE the best for now! Thanx for such a great tool!

@pyscripter
Copy link
Owner

it is correct given my comment about daemon threads. If you run your script in a python interpreter the daemon thread should stop when main exits.
With Pyscripter avoid daemon threads and try to join the threads before exiting main.

@jbelusic
Copy link
Author

jbelusic commented May 13, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants