On Thu, Jan 5, 2023 at 08:17 PM, GreenGreen wrote:
Even after reading buddy response, I am still puzzled why my code did not
work.
If you read the man page for fork carefully you'll see it says... The child process and the parent process run in separate memory spaces. And, J¨¹rgen kindly pointed out that the API works well when all threads run within the same process and have access to the same memory and global variables. So...
when you call:
p = Process(target = app.run)
p.start()
p.join()
and then, subsequently:
app.reqMktData(101, contract , '', False, False, [])
time.sleep(2)
app.disconnect()
You just might not be calling the functions in the process you think you are. In other words, you need to make sure you're referring to the same "app", because now there are two (one in the child and one in the parent).
So, the problem just might be that you're having the parent operate on things that are actually inside the child (or vice versa) and without doing some special IPC like shared memory etc this won't work. And even then, be careful because the API may not be designed to operate that way necessarily.
If you want the parent to "control" the lives of the children (count them, spawn a few, etc) that's one thing. But you can't just assume they share everything off the bat.. they share very little. A fork basically just starts a copy of the parent process in separate address space.
Think of it like running the same program twice from the command-line. If you did that you'd need a different client ID for each and two different tcp/ip sockets and probably call reqMktData on different contracts, right?
This is why I suggested you get more familiar with what fork does, since under the hood that's likely what a new Python Process
does... just fork basically. Does this help?
Anyway, at the end of the day when J¨¹rgen asks What is it that you are trying to achieve? he's being practical. However, when I suggested you read the man page for fork it was for you to learn something in an academic sense.
For the life of me IDK why you'd actually want to complicate things in the way you're trying to, lol. There's nothing wrong with using multiprocessing, I actually prefer it. But it's always easier to first use the facilities built into the OS before rewriting things in Python... you know, top, kill, pgrep, Unix pipes, the shell's ampersand operator etc. Then, if you're doing the same thing over and over whip something up in Python.
Just my 2cents. Good luck!