Python Forking: Why Wait for User Input?

  • Thread starter daniel_i_l
  • Start date
  • Tags
    Python
In summary, when forking a process or starting a thread in Python and waiting for user input, the forked process will not print anything to the screen until a newline character is printed. However, if the third line is changed to print 'test' without a comma, it will print. This could be due to threads or processes accessing the same memory and needing to wait their turn to avoid corruption. This is similar to a mutex situation.
  • #1
daniel_i_l
Gold Member
868
0
For some reasons, if I fork a process (or start a thread) in python and let the original program wait for user input, the the forked process doesn't print anything to the screen until the newline character is printed. For example (i used '----' instead of indent):
...
ret = os.fork()
if ret == 0:
----print 'test',
else:
----data = raw_input('input: ')
doesn't print any thing to the screen. But if the 3rd line is instead
print 'test' (without comma)
then it does.
Why is this?
Thanks
 
Technology news on Phys.org
  • #2
I'm no expert in this, but this reminds me of mutex situation.

Often in programs, different threads or processes use the same memory, so in order to avoid corruption, when a piece of code accesses this memory, it locks it to other code until its done.

This sounds exactly to be the case with your console you are running your code on. Threads and processes need to wait their turn, but I could be wrong, but to me this most likely seems to be the case.
 
  • #3


As a scientist, it is important to understand the underlying mechanisms and processes in any programming language. In this case, the issue lies in the way that the print function and the fork() system call work in Python.

When you use the comma in the print statement, it changes the way that the output is buffered. This means that the print statement will wait for other output to be printed before displaying the result. In this case, the forked process is waiting for the main process to finish before displaying its output.

On the other hand, when you remove the comma, the print statement does not wait for other output and will display its result immediately. This allows the forked process to display its output even while the main process is waiting for user input.

Overall, this is a common issue in programming and can be solved by understanding the buffering processes and using the appropriate print statement for the desired outcome. It is important to carefully consider the order of operations and how different functions interact with each other in order to avoid unexpected results.
 

Related to Python Forking: Why Wait for User Input?

1. What is forking in Python?

Forking in Python is a technique used to create a new process from an existing one. This allows the original process to continue running while the new process executes a different set of instructions.

2. Why is forking necessary in Python?

Forking is useful in situations where a process needs to wait for user input. Instead of halting the entire program, the original process can continue running while the new process handles the user input. This allows for more efficient use of resources and prevents the program from becoming unresponsive.

3. How does the forking process work in Python?

In Python, forking works by using the os.fork() function. This function creates a new process by duplicating the existing one. The new process inherits all the resources and attributes of the original process, but can execute a different set of instructions.

4. What are the benefits of using forking in Python?

Using forking in Python can improve the performance and efficiency of a program. It allows for asynchronous processing, where multiple tasks can be executed at the same time. It also helps prevent the program from becoming unresponsive, as the original process can continue running while the new process handles other tasks.

5. Are there any drawbacks to using forking in Python?

One potential drawback of forking in Python is that it can be complex and difficult to debug. If not implemented correctly, it can lead to issues such as memory leaks or deadlocks. It also requires careful consideration of how data is shared between the original process and the new process.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
18
Views
2K
Replies
17
Views
8K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
Back
Top